[leet code] Populating Next Right Pointers in Each Node II
2014-01-19 08:25
489 查看
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:
============
Analysis:
1. Recursive approach
2. Each recursive call focus on one node
Accordingly, we here are different cases while handling one node:
For left child (if left child exist):
left child -> right child, if right child exist
left child -> null, if current node is the right most node && no right child
left child -> next available node in the same level
left child -> null if no next node available in the sam level
For right child(if right child exist):
right child -> null if current node is the right most node
right child -> next available node in the same level
right child -> null if no next node available in the same level
!!! Note that during recursive call, we must deal with right child then left child. Otherwise, the "next" links of the nodes on the right hand side of the same level would not have been established when program finding the next available node in the same
level.
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
============
Analysis:
1. Recursive approach
2. Each recursive call focus on one node
Accordingly, we here are different cases while handling one node:
For left child (if left child exist):
left child -> right child, if right child exist
left child -> null, if current node is the right most node && no right child
left child -> next available node in the same level
left child -> null if no next node available in the sam level
For right child(if right child exist):
right child -> null if current node is the right most node
right child -> next available node in the same level
right child -> null if no next node available in the same level
!!! Note that during recursive call, we must deal with right child then left child. Otherwise, the "next" links of the nodes on the right hand side of the same level would not have been established when program finding the next available node in the same
level.
/** * 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; helper(root); return; } public void helper(TreeLinkNode node){ if (node == null) return; // handle left child if (node.left != null){ if (node.right != null) node.left.next = node.right; else{ // has left child no right child if (node.next != null){ TreeLinkNode temp = node.next; while(temp.next != null && temp.left == null && temp.right == null) temp = temp.next; if (temp.left != null){ node.left.next = temp.left; }else if(temp.right != null){ node.left.next = temp.right; }else { node.left.next = null; } } else node.left.next = null; } } // handle right child if (node.right != null){ if (node.next != null){ TreeLinkNode temp = node.next; while(temp.next != null && temp.left == null && temp.right == null) temp = temp.next; if (temp.left != null){ node.right.next = temp.left; }else if(temp.right != null){ node.right.next = temp.right; }else { node.right.next = null; } } else node.right.next = null; } helper(node.right); // !!! must right first helper(node.left); } }
相关文章推荐
- LeeCode_Populating Next Right Pointers in Each Node II
- [leet code] Populating Next Right Pointers in Each Node
- CODE 16: Populating Next Right Pointers in Each Node II
- leecode 解题总结:117. Populating Next Right Pointers in Each Node II
- LC117 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】Populating Next Right Pointers in Each Node II
- LeetCode(117) Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node I & II
- 117. Populating Next Right Pointers in Each Node II
- 117:Populating Next Right Pointers in Each Node II【树】【DFS】
- LeetCode(49) Populating Next Right Pointers in Each Node I II
- leetcode--Populating Next Right Pointers in Each Node II
- [LeetCode73]Populating Next Right Pointers in Each Node II
- [LeetCode]Populating Next Right Pointers in Each Node I & II
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
- [LeetCode]Populating Next Right Pointers in Each Node II