Populating Next Right Pointers in Each Node II
来源:互联网 发布:php开发实战权威指南 编辑:程序博客网 时间:2024/05/18 20:51
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 不同的是给定的二叉树不是一棵完全二叉树,而是一棵任意的很普通的二叉树。但是思路也差不多,也是根据上一行(next指针已经被修改好)来修改下一行的结点的next指针。
- 根据上一行的结点,首先找出上一行的结点中有孩子的第一个结点,并记录该结点的第一个孩子,以用做下次循环的上一行的第一个结点
- 对于下一行,用一个last指针来记录当前已经被修改好的链表的最后一个结点,不断更新last,那么该行结点也就修改好了
3.继续循环
例如:给出的树
- 第一行结点为1,根据第一行结点来修改第二行,先找出第一行中第一个有孩子的结点,第一行中第一个有孩子的结点为1,然后记录该节点的第一个孩子2, 即L=2。
- last=2,如果last指向1的左孩子,此时如果1有右孩子的话,显然1的右孩子为3,那么更新last.next=3,last=last.next
- 1的孩子们已经修改好了,那么修改1的next结点的孩子们,但是1的next为null,接下来根据第二行的结点,来修改他们的孩子结点,并且是从第二行的L结点开始修改的,因为L是第一个有孩子的结点
- 同上,找出第二行中第一个有孩子的结点,即为2,然后记录它的第一个孩子,L=4
- last=4,其中2有右孩子5,那么last.next=5,last=last.next,2的孩子们已经更新好了,那么开始更新2.next结点
- 2.next结点为3, 如果3 存在左孩子,那么last=3.left,last=last.next,显然3是没有左孩子的,但是3 有右孩子,那么last=3.right 。这样3的孩子就更新完了。接下来更新3.next的孩子们,但是3.next是空,所以第三行也就更新完了
- 根据第三行来更新第四行,但是第四行中全部为空,已经在第三行中找不到第一个有孩子的结点了,所以第四行不用更新,直接返回即可。
public static void connect(TreeLinkNode root) { if (root == null) return; while (root != null) { TreeLinkNode firstNode = root; TreeLinkNode l = null; TreeLinkNode last = null; while (firstNode != null && !hasSon(firstNode)) firstNode = firstNode.next; if (firstNode == null) return; if (firstNode.left != null) l = firstNode.left; else l = firstNode.right; last = l; if (last == firstNode.left && firstNode.right != null) { last.next = firstNode.right; last = last.next; } firstNode = firstNode.next; while (firstNode != null) { if (firstNode.left != null) { last.next = firstNode.left; last = last.next; } if (firstNode.right != null) { last.next = firstNode.right; last = last.next; } firstNode = firstNode.next; } root = l; } } public static boolean hasSon(TreeLinkNode root) { return root.left != null || root.right != null; }
0 0
- Populating Next Right Pointers in Each Node、Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node 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] 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
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node II
- eclipse环境 Cannot run program "C:\Java\jdk1.6.0_02\bin\javaw.exe" (in directory "D:\project\SDK"): Cr
- after Normal block(#47) at 0x001D3908 错误的一次解决经历
- Java中属性和字段的区别
- 深入C++之const对象行为
- JVM加载class文件的原理
- Populating Next Right Pointers in Each Node II
- selendroid
- HDU 月之数
- SpringMVC接受Form传值和与jsp传值
- string data,right truncation
- install4j 打包 spark(1)-install4j-6.0
- spring事务管理
- zookeeper——leader选举
- 建设局安全施工监控指挥系统