-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.py
75 lines (74 loc) · 2.1 KB
/
flatten.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
prev = None
class Solution:
# @param {TreeNode} root
# @return {void} Do not return anything, modify root in-place instead.
def flatten(self, root):
if root == None:
return
stack = [root]
res = []
p = root
while(len(stack)!=0):
if p == None:
# pop
p = stack.pop()
res.append(p)
if len(stack) == 0:
break
top = stack[len(stack)-1]
if top.left ==p or top.right == p:
p = None # continue pop
else:
p = top #
else:
# push
if p.left != None:
stack.append(p.left)
if p.right != None:
stack.append(p.right)
if p.right != None:
p = p.right
elif p.left!=None:
p = p.left
else :
p = None
newroot=TreeNode(0)
q= newroot
while(len(res)!=0):
cur = res.pop()
cur.left = None
q.right = cur
q = cur
root = newroot.right
def flatten2(self, root):
if not root:
return
node = root
while(node):
if node.left:
pre = node.left
while(pre.right):
pre = pre.right
pre.right = node.right
node.right = node.left
node.left = None
node = node.right
def flatten3(self, root):
global prev
prev= None
def flattenHelper(node):
if not node :
return
flattenHelper(node.right)
flattenHelper(node.left)
global prev
node.right = prev
node.left = None
prev= node
flattenHelper(root)