-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path116.填充每个节点的下一个右侧节点指针.py
53 lines (46 loc) · 1.17 KB
/
116.填充每个节点的下一个右侧节点指针.py
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
50
51
52
#
# @lc app=leetcode.cn id=116 lang=python
#
# [116] 填充每个节点的下一个右侧节点指针
#
# @lc code=start
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
result = []
stack = [root]
result = self.bfs(result,stack)
return root
def bfs(self,result, stack):
new_stack = []
while stack:
node = stack.pop(0)
if node == None:
continue
result.append(node.val)
#print(result)
if node.left:
new_stack.append(node.left)
if node.right:
new_stack.append(node.right)
if stack:
node.next = stack[0]
else:
node.next = None
stack = new_stack[:]
result.append("#")
new_stack = []
return result
# @lc code=end