117. Populating Next Right Pointers in Each Node II
Follow up for problem “Populating Next Right Pointers in Each Node”.
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
1 | For example, |
题意:
题意同116. Populating Next Right Pointers in Each Node,题目做了一些更改:就是树不一定是满二叉树。
思路:
思路同116. Populating Next Right Pointers in Each Node,解法的核心:递推思想依然不需要改变,依然是依据当前层的next 指针,设置下一层的 next 指针。只是找结点麻烦些,我们定义了两个函数,findNextLevelNextNode(root, curNode)用来找(n+1)层的下一个节点,findNextLevelStartNode(root)用来找下一层的起始节点。
1 | class Solution { |