Skip to content

Commit e7b47f6

Browse files
committed
Populating Next Right Pointers in Each Node II
1 parent 9a14e1d commit e7b47f6

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
3+
4+
Given a binary tree
5+
struct Node {
6+
int val;
7+
Node *left;
8+
Node *right;
9+
Node *next;
10+
}
11+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be
12+
set to NULL.
13+
Initially, all next pointers are set to NULL.
14+
15+
Follow up:
16+
You may only use constant extra space.
17+
Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
18+
19+
Example 1:
20+
Input: root = [1,2,3,4,5,null,7]
21+
Output: [1,#,2,3,#,4,5,7,#]
22+
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its
23+
next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers,
24+
with '#' signifying the end of each level.
25+
26+
Constraints:
27+
The number of nodes in the given tree is less than 6000.
28+
-100 <= node.val <= 100
29+
"""
30+
"""
31+
# Definition for a Node.
32+
class Node:
33+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
34+
self.val = val
35+
self.left = left
36+
self.right = right
37+
self.next = next
38+
"""
39+
40+
class Solution:
41+
def connect(self, root: 'Node') -> 'Node':
42+
if not root:
43+
return
44+
45+
self.dfs(root)
46+
return root
47+
48+
49+
def dfs(self, root, parent=None):
50+
if not root:
51+
return
52+
cur1 = cur2 = None
53+
while parent:
54+
55+
if not parent.left and not parent.right:
56+
parent = parent.next
57+
continue
58+
59+
if not cur1:
60+
cur1 = parent.left or parent.right
61+
if cur1 == parent.right:
62+
parent = parent.next
63+
64+
elif not cur2:
65+
if cur1 == parent.right or cur1 == parent.left and not parent.right:
66+
parent = parent.next
67+
continue
68+
if cur1 == parent.left:
69+
cur2 = parent.right
70+
else:
71+
cur2 = parent.left or parent.right
72+
73+
else:
74+
cur1.next = cur2
75+
cur1, cur2 = cur2, None
76+
77+
self.dfs(root.left, root)
78+
self.dfs(root.right, root)
79+
80+
81+
class Solution1:
82+
def connect(self, root: 'Node') -> 'Node':
83+
if not root:
84+
return
85+
86+
self.dfs(root)
87+
return root
88+
89+
90+
def dfs(self, root, next_node=None):
91+
if not root:
92+
return
93+
94+
root.next = next_node
95+
96+
n1 = root.right
97+
n2 = root.next.left if root.next else None
98+
n3 = root.next.right if root.next else None
99+
next_node = n1 or n2 or n3
100+
if root.left:
101+
self.dfs(root.left, next_node)
102+
if root.right:
103+
if next_node == n1:
104+
next_node = n2 or n3
105+
elif next_node == n2:
106+
next_node = n3
107+
else:
108+
next_node = None
109+
self.dfs(root.right, next_node)

0 commit comments

Comments
 (0)