力扣 114. 二叉树展开为链表

前置

1
2
3
4
5
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def flatten(root):
"""
Do not return anything, modify root in-place instead.
"""
def order(node):
if not node:
return
order(node.left)
order(node.right)
if node.left:
next_ = node.right if node.right else None
node.left, node.right = None, node.left
if next_:
cur = node.right
while cur.right:
cur = cur.right
cur.right = next_
order(root)
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信