File tree 1 file changed +51
-0
lines changed
1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution (object ):
9
+ # stack
10
+ def flatten (self , root ):
11
+ """
12
+ :type root: TreeNode
13
+ :rtype: void Do not return anything, modify root in-place instead.
14
+ """
15
+ if root is None :
16
+ return
17
+ if root .left is None and root .right is None :
18
+ return
19
+ current = root
20
+ stack = [root ]
21
+ while stack :
22
+ node = stack .pop ()
23
+ self .appendNode (stack , node .right )
24
+ self .appendNode (stack , node .left )
25
+ if current != node :
26
+ current .right = node
27
+ current .left = None
28
+ current = node
29
+
30
+ def appendNode (self , stack , node ):
31
+ if node :
32
+ stack .append (node )
33
+
34
+ # recursive
35
+ # https://discuss.leetcode.com/topic/11444/my-short-post-order-traversal-java-solution-for-share/2
36
+ # def __init__(self):
37
+ # self.prev = None
38
+ #
39
+ # def flatten(self, root):
40
+ # if root is None:
41
+ # return
42
+ # self.flatten(root.right)
43
+ # self.flatten(root.left)
44
+ # root.right = self.prev
45
+ # root.left = None
46
+ # self.prev = root
47
+
48
+
49
+
50
+
51
+
You can’t perform that action at this time.
0 commit comments