题目:node
Given a binary treeide
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
.spa
Note:code
For example,
Given the following perfect binary tree,blog
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 -> NULL
Tree Depth-first Search
连接: http://leetcode.com/problems/populating-next-right-pointers-in-each-node/ci
题解:leetcode
使用DFS。题目给出彻底二叉树,因此只要先判断next节点是否为空,接下来断定root的左右子节点是否为空就能够了。get
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