Populating Next Right Pointers in Each Node
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. 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.
(连接完全二叉树同一层的结点)
Example:
1. 递归
在递归的过程中需要考虑左、右子树,兄弟结点之间的连接。具体实现过程如下:
1 | """ |
2. 迭代
由于在递归的过程中会使用到栈,因此空间复杂度为 O(log(n)),为了实现空间复杂度为 O(1),使用迭代的方法,具体实现过程如下:
1 | """ |