You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
1 | struct Node { |
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Follow up:
- You may only use constant extra space.
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
Example 1:
1 | Input: root = [1,2,3,4,5,6,7] |
Constraints:
- The number of nodes in the given tree is less than
4096
. -1000 <= node.val <= 1000
递归
看到这个题之后的第一反应就是按层处理, 把每一层的所有节点都放到一个队列中, 然后挨个把他们连接起来. 后来发现题目要求不能使用除递归外的额外空间. 所以只能用递归做了
后来想了一会, 发现递归好像比队列更加简单.
首先递归边界是null或者叶子节点.
然后connect(root)就等价于先connect两个子树, 然后再把左子树最右侧的节点与右子树最左侧的节点连接起来
左子树a的所有最右侧节点定义为a, a.right, a.right.right, ....
右子树b的所有最左侧节点定义为b, b.left, b.left.left, ....
1 | /* |