116 Populating Next Right Pointers in Each Node 填充每個(gè)節(jié)點(diǎn)的下一個(gè)右側(cè)節(jié)點(diǎn)指針
Description:
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *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.
Follow up:
You may only use constant extra space.
Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
Example:
Example 1:
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
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. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
Constraints:
The number of nodes in the given tree is less than 4096.
-1000 <= node.val <= 1000
題目描述:
給定一個(gè)完美二叉樹坷备,其所有葉子節(jié)點(diǎn)都在同一層辈讶,每個(gè)父節(jié)點(diǎn)都有兩個(gè)子節(jié)點(diǎn)。二叉樹定義如下:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每個(gè) next 指針粪牲,讓這個(gè)指針指向其下一個(gè)右側(cè)節(jié)點(diǎn)雾鬼。如果找不到下一個(gè)右側(cè)節(jié)點(diǎn)葱轩,則將 next 指針設(shè)置為 NULL。
初始狀態(tài)下狠毯,所有 next 指針都被設(shè)置為 NULL护糖。
示例 :
輸入:{"id":"2","left":{"
id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"
id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}
輸出:{"id":"2","left":{"
id":"4","left":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":{"
ref":"5"},"next":null,"right":{"
ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}
解釋:給定二叉樹如圖 A 所示,你的函數(shù)應(yīng)該填充它的每個(gè) next 指針嚼松,以指向其下一個(gè)右側(cè)節(jié)點(diǎn)嫡良,如圖 B 所示。
提示:
你只能使用常量級(jí)額外空間惜颇。
使用遞歸解題也符合要求皆刺,本題中遞歸程序占用的棧空間不算做額外的空間復(fù)雜度凌摄。
思路:
- 遞歸法
左子樹的下一個(gè)是右子樹
如果有 next指針, 則右子樹的下一個(gè)是 next的左子樹 - 迭代法
類似層序遍歷, 每次將下一層的 next指針連接好, 設(shè)置一個(gè)指針指向每一層的第一個(gè)節(jié)點(diǎn)
每一層遍歷時(shí), 按 next指針移動(dòng)
next指針更新方式與遞歸法相同
時(shí)間復(fù)雜度O(n), 空間復(fù)雜度O(1)
代碼:
C++:
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution
{
public:
Node* connect(Node* root)
{
if (!root or !root -> left) return root;
root -> left -> next = root -> right;
if (root -> next) root -> right -> next = root -> next -> left;
connect(root -> left);
connect(root -> right);
return root;
}
};
Java:
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if (root == null) return root;
Node first = root, cur = null;
while (first.left != null) {
cur = first;
while (cur != null) {
cur.left.next = cur.right;
if (cur.next != null) cur.right.next = cur.next.left;
cur = cur.next;
}
first = first.left;
}
return root;
}
}
Python:
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root or not root.left:
return root
root.left.next = root.right
if root.next:
root.right.next = root.next.left
self.connect(root.left)
self.connect(root.right)
return root