File tree Expand file tree Collapse file tree 1 file changed +14
-15
lines changed Expand file tree Collapse file tree 1 file changed +14
-15
lines changed Original file line number Diff line number Diff line change 1
1
# Definition for a binary tree node.
2
2
# class TreeNode(object):
3
- # def __init__(self, x):
4
- # self.val = x
5
- # self.left = None
6
- # self.right = None
7
-
3
+ # def __init__(self, val=0, left=None, right=None):
4
+ # self.val = val
5
+ # self.left = left
6
+ # self.right = right
8
7
class Solution (object ):
9
8
def flatten (self , root ):
10
9
"""
11
10
:type root: TreeNode
12
11
:rtype: None Do not return anything, modify root in-place instead.
13
12
"""
14
- if not root :
13
+ if not root or ( not root . left and not root . right ) :
15
14
return root
15
+
16
16
self .flatten (root .left )
17
17
self .flatten (root .right )
18
-
19
- tmp = root .right
18
+
19
+ tmp = root .right
20
20
root .right = root .left
21
- root .left = None
22
-
23
- node = root
24
- while node .right :
25
- node = node .right
26
- node .right = tmp
27
-
21
+ root .left = None
22
+
23
+ while root and root .right :
24
+ root = root .right
25
+
26
+ root .right = tmp
You can’t perform that action at this time.
0 commit comments