-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
26 lines (23 loc) · 744 Bytes
/
solution.py
File metadata and controls
26 lines (23 loc) · 744 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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
rights = []
node = root
while node:
if node.left and node.right:
rights.append(node.right)
if node.left:
node.right = node.left
node.left = None
elif len(rights) > 0 and not node.right:
node.right = rights.pop()
node = node.right