[Leetcode][JAVA] Populating Next Right Pointers in Each Node II
2015-01-08 06:37
756 查看
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,
After calling your function, the tree should look like:
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)因为函数中新建的辅助节点所占内存在出函数中会被释放。 代码:
public void connect(TreeLinkNode root) { TreeLinkNode levelStart = root; while(levelStart!=null) levelStart = connectChildren(levelStart); } public TreeLinkNode connectChildren(TreeLinkNode root) { TreeLinkNode cur = root; TreeLinkNode helper = new TreeLinkNode(0); TreeLinkNode children = helper; while(cur!=null) { if(cur.left!=null) { children.next = cur.left; children = children.next; } if(cur.right!=null) { children.next = cur.right; children = children.next; } cur = cur.next; } return helper.next; }
相关文章推荐
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
- [LeetCode][Java] Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II (Java)
- leetcode:Populating Next Right Pointers in Each Node II 【Java】
- 【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
- Populating Next Right Pointers in Each Node II leetcode java
- LeetCode-Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- [leetcode刷题系列]Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- leetcode_question_117 Populating Next Right Pointers in Each Node II
- LeetCode:Populating Next Right Pointers in Each Node I,II 编程之美3.10
- leetcode Populating Next Right Pointers in Each Node II
- Leetcode_Populating Next Right Pointers in Each Node II
- LeetCode - Populating Next Right Pointers in Each Node II
- LeetCode_Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- LeetCode-Populating Next Right Pointers in Each Node II
- [leetcode]Populating Next Right Pointers in Each Node II