Populating Next Right Pointers in Each Node II
Given a binary tree, 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. 递归
首先根据根节点的 next 指针找到这个子树的孩子节点的 next 需要连接的位置,然后将孩子节点通过 next 连接起来,再处理子树部分。具体实现过程如下:
1 | """ |
2. 迭代
根据 BFS 进行层次遍历,具体实现过程如下:
1 | """ |