-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathflattenbinarytree.py
More file actions
40 lines (27 loc) · 986 Bytes
/
flattenbinarytree.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
class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
def preorder(current):
if current==None:
return None
leftn=preorder(current.left)
rightn=preorder(current.right)
if current.left!=None:
temp=current.right
current.right=current.left
current.left=None
leftn.right=temp
if rightn!=None:
return rightn
if leftn!=None:
return leftn
return current
preorder(root)