题目:
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / 2 3 / / 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / 2 -> 3 -> NULL / / 4->5->6->7 -> NULLTree Depth-first Search
链接: http://leetcode.com/problems/populating-next-right-pointers-in-each-node/
题解:
使用DFS。题目给出完全二叉树,所以只要先判断next节点是否为空,接下来判定root的左右子节点是否为空就可以了。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution { public void connect(TreeLinkNode root) { if(root == null) return; TreeLinkNode node = root.next; if(node != null){ if(node.right != null){ root.right.next = node.left; root.left.next = root.right; } } else { if(root.right != null){ root.right.next = null; root.left.next = root.right; } } connect(root.left); connect(root.right); } }
Update:
之前写得好丑..精简一下。递归的Space Complexity怎么算? 这里建立了一个TreeLinkNode,是reference type,这个object的reference存在stack里,但object是存在heap里。 还要学习JVM的许多知识。
Time Complexity - O(n), Space Complexity - O(1).
/** * 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; TreeLinkNode p = root.next; if(root.left != null) root.left.next = root.right; if(root.right != null) root.right.next = (p == null) ? null : p.left; connect(root.left); connect(root.right); } }
二刷:
Java:
/** * 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; } TreeLinkNode p = root.next; if (root.left != null) { root.left.next = root.right; root.right.next = p == null ? null : p.left; } connect(root.left); connect(root.right); } }
三刷:
跟之前的方法一样使用了recursive。但其实iterative的solution更好。
Java:
Recursive:
Time Complexity - O(n), Space Complexity - O(n)
/** * 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; if (root.right != null) { root.left.next = root.right; if (root.next != null) root.right.next = root.next.left; } connect(root.left); connect(root.right); } }
Iterative:
解法来自 yavinci大神。
两层循环,当root和root.left不为空的时候,我们要利用好自己创建的next节点来不断向右进行遍历。遍历完这一层以后我们可以设置root = root.left,这样我们到了下一层,继续向右进行遍历。
Time Complexity - O(n), Space Complexity - O(1)
/** * 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) { while (root != null && root.left != null) { TreeLinkNode cur = root; while (cur != null) { cur.left.next = cur.right; if (cur.next != null) cur.right.next = cur.next.left; cur = cur.next; } root = root.left; } } }
Update:
/** * 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) { TreeLinkNode curLevel = new TreeLinkNode(-1); TreeLinkNode newLevel = curLevel; while (root != null) { if (root.left != null) { curLevel.next = root.left; curLevel = curLevel.next; } if (root.right != null) { curLevel.next = root.right; curLevel = curLevel.next; } root = root.next; if (root == null) { curLevel = newLevel; root = newLevel.next; newLevel.next = null; } } } }
Reference:
https://leetcode.com/discuss/7327/a-simple-accepted-solution