[Leetcode][JAVA] 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.

 

For example,
Given the following binary tree,

         1
       /        2    3
     / \        4   5    7

 

After calling your function, the tree should look like:

         1 -> NULL
       /        2 -> 3 -> NULL
     / \        4-> 5 -> 7 -> NULL这道题是Populating Next Right Pointers in Each Node的加强版,更适合一般情况,所以这道题的解法也适合Populating Next Right Pointers in Each Node。对每一层,如果这一层节点已经连接好,那么可以通过next指针遍历这一层的所有节点,所以,也就可以按顺序遍历到这些节点的所有子节点,那么,也就可以按顺序将这些子节点连接。问题是,连接好的子节点链头需要被记录下来,因为这样才能进行下一次遍历,将这些子节点的子节点们连接起来。这里使用一个函数将这个功能封装起来,输入是待遍历层的头节点,子节点连接后,返回连接好的子节点链头(即下一层的头节点)。函数中在遍历前新建一个辅助节点helper,helper的next指针指向第一个子节点(如果有的话)。这样,返回值就可以通过helper的next指针得到。具体过程为,父层的遍历指针cur遍历到每个节点时,都讨论左子节点和右子节点。子层的遍历指针children从helper开始,遇到当前父层节点有左子就把当前children的next指针指向左子并把children指向左子。如果有右子也是一样。如此可以将所有子节点连接。对所有层从上往下进行这样的函数调用,则可以把整个树每层子节点都连接起来。

注意,空间复杂度依然为O(1)因为函数中新建的辅助节点所占内存在出函数中会被释放。代码:
 1     public void connect(TreeLinkNode root) {
 2         TreeLinkNode levelStart = root;
 3         while(levelStart!=null)
 4             levelStart = connectChildren(levelStart);
 5     }
 6     public TreeLinkNode connectChildren(TreeLinkNode root) {
 7         TreeLinkNode cur = root;
 8         TreeLinkNode helper = new TreeLinkNode(0);
 9         TreeLinkNode children = helper;
10         while(cur!=null) {
11             if(cur.left!=null) {
12                children.next = cur.left;
13                children = children.next;
14             }
15             if(cur.right!=null) {
16                 children.next = cur.right;
17                 children = children.next;
18             }
19             cur = cur.next;
20         }
21         return helper.next;
22     }
时间: 2024-10-05 02:57:50

[Leetcode][JAVA] Populating Next Right Pointers in Each Node II的相关文章

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could

LeetCode[Tree]: 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. For example, Given the following binary t

【leetcode】Populating Next Right Pointers in Each Node II

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 s

Java for LeetCode 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. For example, Given the following binary t

leetcode 117 Populating Next Right Pointers in Each Node II ----- java

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. For example,Given the following binary tr

LeetCode 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. For example, Given the following binary t

[C++]LeetCode: 124 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. For example, Given the following bina

Populating Next Right Pointers in Each Node II leetcode java

题目: 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. For example, Given the following bina

[LeetCode][JavaScript]Populating Next Right Pointers in Each Node

Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next