Populating Next Right Pointers in Each Node
2013-08-19 01:01
357 查看
//recursive, log(n) space cpmplexity public void connect(TreeLinkNode root) { // Start typing your Java solution below // DO NOT write main() function if(root == null) return; if(root.left != null) root.left.next = root.right; if(root.right != null) root.right.next = (root.next == null) ? null : root.next.left; connect(root.left); connect(root.right); } //iterative, constant space cpmplexity public void connect(TreeLinkNode root) { // Start typing your Java solution below // DO NOT write main() function while(root != null) { TreeLinkNode tmp = root; while(tmp != null) { if(tmp.left != null) tmp.left.next = tmp.right; if(tmp.right != null && tmp.next != null) tmp.right.next = tmp.next.left; tmp = tmp.next; } root = root.left; } }
相关文章推荐
- LeetCode刷题笔录Populating Next Right Pointers in Each Node II
- leetcode116 Populating Next Right Pointers in Each Node
- LEETCODE: Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- [leetcode] Populating Next Right Pointers in Each Node II
- 【LeetCode with Python】 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
- leecode 解题总结:116. Populating Next Right Pointers in Each Node
- LeetCode_Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node ---LeetCode
- Populating Next Right Pointers in Each Node题解
- Populating Next Right Pointers in Each Node II (Java)
- Populating Next Right Pointers in Each Node
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
- Leetcode——populating-next-right-pointers-in-each-node
- LeetCode 117 Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II