-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN114二叉树到链表.py
More file actions
34 lines (28 loc) · 886 Bytes
/
N114二叉树到链表.py
File metadata and controls
34 lines (28 loc) · 886 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
# 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: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
if not root or not root.right and not root.left:
return
cur = root
while cur:
if cur.left:
if not cur.right:
cur.right = cur.left
cur.left = None
cur = cur.right
else:
pre = cur.left
while pre.right:
pre = pre.right
pre.right = cur.right
cur.right = None
else:
cur = cur.right