Populating Next Right Pointers in Each Node II
2014-07-12 09:57
344 查看
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:
这道题目用bfs 和 dfs 都可以做。更推荐bfs。如果用dfs ,则需要注意的 是 要先遍历右子树,再遍历左子树。因为对左子树的操作,用到了右子树的结果。
下面是我写的dfs,后面是答案的bfs
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
这道题目用bfs 和 dfs 都可以做。更推荐bfs。如果用dfs ,则需要注意的 是 要先遍历右子树,再遍历左子树。因为对左子树的操作,用到了右子树的结果。
下面是我写的dfs,后面是答案的bfs
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ public class Solution { public void connect(TreeLinkNode root) { if (root == null) { return; } root.next = null; find(root, root.right); find(root, root.left); } private void find(TreeLinkNode parent, TreeLinkNode cur) { if (cur == null) { return; } if (parent.right != null && cur != parent.right) { cur.next = parent.right; } else { TreeLinkNode temp = parent.next; while (temp != null) { if (temp.left != null) { cur.next = temp.left; break; } else if (temp.right != null) { cur.next = temp.right; break; } else{ temp = temp.next; } } if (temp == null) { cur.next = null; } } if (cur.right != null) { find(cur, cur.right); } if (cur.left != null) { find(cur, cur.left); } } }
public class Solution { public void connect(TreeLinkNode root) { if (root == null) { return; } TreeLinkNode parent = root; TreeLinkNode pre; TreeLinkNode next; while (parent != null) { pre = null; next = null; while (parent != null) { if (next == null){ next = (parent.left != null) ? parent.left: parent.right; } if (parent.left != null){ if (pre != null) { pre.next = parent.left; pre = pre.next; } else { pre = parent.left; } } if (parent.right != null) { if (pre != null) { pre.next = parent.right; pre = pre.next; } else { pre = parent.right; } } parent = parent.next; } parent = next; } } }
相关文章推荐
- [Leetcode]Populating Next Right Pointers in Each Node I && II
- leetcode:Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II <leetcode>
- Problem 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 II
- FTPrep, 117 Populating Next Right Pointers in Each Node II,总结得很精准。
- 5.1.12 Populating Next Right Pointers in Each Node II
- [leedcode 117] 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
- 117. Populating Next Right Pointers in Each Node II
- leetcode[117]: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 @ Python
- 117. Populating Next Right Pointers in Each Node II