- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathflattenbinarytree.py
More file actions
Latest commit
40 lines (27 loc) · 986 Bytes
/
Copy pathflattenbinarytree.py
File metadata and controls
40 lines (27 loc) · 986 Bytes
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
classSolution:
defflatten(self, root: Optional[TreeNode]) ->None:
"""
Do not return anything, modify root in-place instead.
"""
defpreorder(current):
ifcurrent==None:
returnNone
leftn=preorder(current.left)
rightn=preorder(current.right)
ifcurrent.left!=None:
temp=current.right
current.right=current.left
current.left=None
leftn.right=temp
ifrightn!=None:
returnrightn
ifleftn!=None:
returnleftn
returncurrent
preorder(root)