题目描述
Output: {“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"1","left":{"id”:“2”,“left”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“4”,“left”:null,“next”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“6”,“left”:null,“next”:null,“right”:null,“val”:7},“right”:null,“val”:6},“right”:null,“val”:5},“right”:null,“val”:4},“next”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"7","left":{"ref”:“5”},“next”:null,“right”:{“KaTeX parse error: Expected 'EOF', got '}' at position 9: ref":"6"}̲,"val":3},"righ…ref”:“4”},“val”:2},“next”:null,“right”:{"$ref":“7”},“val”:1}
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.
题目链接
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
方法思路
Approach1 : level-traversal with Queue 这个方法基于所给二叉树是完美二叉树的基础。
空间复杂度好像不太符合题目要求 tricky!
class Solution {
//Runtime: 2 ms, faster than 15.01%
//Memory Usage: 35 MB, less than 93.85%
public Node connect(Node root) {
if(root == null) return root;
Queue<Node> q1 = new LinkedList<>();
Queue<Node> q2 = new LinkedList<>();
double depth = 0.0;
q1.offer(root);
while(!q1.isEmpty()){
q2.offer(q1.peek());
Node node = q1.poll();
if(node.left != null) q1.offer(node.left);
if(node.right != null) q1.offer(node.right);
}
while(!q2.isEmpty()){
Node node = q2.poll();
double count = Math.pow(2.0, depth);
while(count-- > 1.0){
node.next = q2.peek();
node = q2.poll();
}
node.next = null;
depth++;
}
return root;
}
}
Approach 2: traversal
class Solution {
//Runtime: 0 ms, faster than 100.00%
//Memory Usage: 35.7 MB, less than 81.92%
public Node connect(Node root) {
if(root == null) return root;
root.next = null;
Node level_start = root;
while(level_start != null){
Node cur = level_start;
while(cur != null){
if(cur.left != null)
cur.left.next = cur.right;
if(cur.right != null && cur.next != null)
cur.right.next = cur.next.left;
cur = cur.next;
}
level_start = level_start.left;
}
return root;
}
}
Approach 3: recursive
class Solution {
//Runtime: 0 ms, faster than 100.00%
//Memory Usage: 36.1 MB, less than 71.97%
public Node connect(Node root) {
if(root == null)
return root;
if(root.left != null){
root.left.next = root.right;
if(root.next != null)
root.right.next = root.next.left;
}
connect(root.left);
connect(root.right);
return root;
}
}
Approach4:
同样是层序遍历,但效率要比方法一高上不少,这个方法同样可用于117. Populating Next Right Pointers in Each Node II
class Solution {
//Runtime: 1 ms, faster than 48.14%
//Memory Usage: 35.9 MB, less than 74.14%
public Node connect(Node root) {
if(root == null) return root;
Queue<Node> q1 = new LinkedList<>();
q1.offer(root);
while(!q1.isEmpty()){
int size = q1.size();
Node node = q1.poll(), cur = node;
while(size-- > 1){
if(node.left != null) q1.offer(node.left);
if(node.right != null) q1.offer(node.right);
cur = q1.poll();
node.next = cur;
node = cur;
}
if(node.left != null) q1.offer(node.left);
if(node.right != null) q1.offer(node.right);
}
return root;
}
}