leedcode做题总结,题目Populating Next Right Pointers in Each NodeI/II---------- 2012/10/28
2014-07-09 18:46
344 查看
这个题可以用先序递归,每个节点把两个儿子连起来,
注意的是每个节点还要把两个子树的每一层连起来,所以我用while来访问子树每一层最边的节点。
由于第一题的思维定势,第二题没想出来,于是参考了别人的代码,使用广度优先历遍,用m记录每一行的第一个,然后让root以它为起点一次next移动,然后用n一次历遍子节点,链接全部子节点。
Update 2015/08/17:
第二道题只需要每次递归最左边的,由于上一行均有next相连,所以我们通过历遍上一行来历遍下一行。
注意的是每个节点还要把两个子树的每一层连起来,所以我用while来访问子树每一层最边的节点。
public void connect(TreeLinkNode root) { if(root!=null){ if(root.left!=null&&root.right!=null){ root.left.next=root.right; TreeLinkNode m =root.left; TreeLinkNode n = root.right; while (m.right!=null){ m.right.next=n.left; m=m.right; n=n.left; } } connect(root.left); connect(root.right); } }
由于第一题的思维定势,第二题没想出来,于是参考了别人的代码,使用广度优先历遍,用m记录每一行的第一个,然后让root以它为起点一次next移动,然后用n一次历遍子节点,链接全部子节点。
public static void conn(TreeLinkNode r){ TreeLinkNode m=null; TreeLinkNode n=null; while (r!=null){ if(r.left!=null){ if(m==null){ m=r.left; n=r.left; }else{ n.next=r.left; n=r.left; } } if(r.right!=null){ if(m==null){ m=r.right; n=r.right; }else{ n.next=r.right; n=r.right; } } r=r.next; } if(m!=null) conn(m); } public void connect(TreeLinkNode root) { conn(root); }
Update 2015/08/17:
public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; TreeLinkNode l = root.left; TreeLinkNode r = root.right; while (l != null && r != null){ l.next = r; l = l.right; r = r.left; } connect(root.left); connect(root.right); } }
第二道题只需要每次递归最左边的,由于上一行均有next相连,所以我们通过历遍上一行来历遍下一行。
public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; TreeLinkNode m = null; TreeLinkNode n = null; while (root != null){ if (root.left != null){ if (m == null){ m = root.left; n = root.left; } else { n.next = root.left; n = n.next; } } if (root.right != null){ if (m == null){ m = root.right; n = root.right; } else { n.next = root.right; n = n.next; } } root = root.next; } connect(m); } }
相关文章推荐
- leecode 解题总结:117. Populating Next Right Pointers in Each Node II
- FTPrep, 117 Populating Next Right Pointers in Each Node II,总结得很精准。
- [leedcode 117] 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
- [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
- LeetCode: Populating Next Right Pointers in Each Node II
- leetcode 79: 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
- 【leetcode】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
- 【ATT】Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II