Leetcode Study Day 35

Populating Next Right Pointers in Each Node II

Given a binary tree

1
2
3
4
5
6
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Example 1:


Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above 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.
Example 2:

Input: root = []
Output: []


Constraints:

The number of nodes in the tree is in the range [0, 6000].
-100 <= Node.val <= 100


Follow-up:

You may only use constant extra space.
The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.

Solution

This post from Leetcode gives a nice and clear solutions.

Full code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
// 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) {
// support variables
Node *currParent = root, *baseChild, *currChild, *nextChild;
while (currParent) {
// skipping childless parents - get a family - up to the last node
while (currParent->next && !currParent->left && !currParent->right) currParent = currParent->next;
// setting the new basechild, provided we have one at all
currChild = baseChild = currParent->left ? currParent->left : currParent->right;
while (currChild) {
// getting nextChild - either the right sibling of currChild or...
if (currParent->right && currChild != currParent->right) nextChild = currParent->right;
// the child of a following parent
else {
// moving to the nextParent, if any
currParent = currParent->next;
// moving parents, if we have too
while (currParent && !currParent->left && !currParent->right) currParent = currParent->next;
// setting nextChild to be the next left/right child, if any; NULL otherwise
nextChild = currParent ? currParent->left ? currParent->left : currParent->right : currParent;
}
currChild->next = nextChild;
currChild = nextChild;
}
// preparing for the next loop
currParent = baseChild;
}
return root;
}
};
  • Copyrights © 2020-2024 Yangyang Cui

请我喝杯咖啡吧~

支付宝
微信