116. 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. The binary tree has the following definition:
1
2
3
4
5
6 struct Node {
int val;
Node *left;
Node *right;
Node *next;
>}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.
递归,当root为null时返回。
如果root有右节点,则左节点next指向右节点。
如果root有右节点同时next已经指向了一个节点,则将其右节点next指向该节点的左子节点。
递归左右子节点,并返回root。
1 | /* |
BFS搜索每一个节点,将节点指向队列中next下一个节点。
当计数器达到2的指数时,将节点指向null。
1 | class Solution { |
116. Populating Next Right Pointers in Each Node
https://xuanhe95.github.io/2022/04/10/116-Populating-Next-Right-Pointers-in-Each-Node/