LeetCode No.114 | StriveZs的博客

LeetCode No.114

LeetCode第114题—二叉树展开为链表

自己代码的开源仓库:click here 欢迎Star和Fork :)

题目描述

给你二叉树的根结点 root ,请你将它展开为一个单链表:

展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
展开后的单链表应该与二叉树 先序遍历 顺序相同。

figure.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
示例 1

输入:root = [1,2,5,3,4,null,6]
输出:[1,null,2,null,3,null,4,null,5,null,6]
示例 2

输入:root = []
输出:[]
示例 3

输入:root = [0]
输出:[0]


提示:

树中结点数在范围 [0, 2000] 内
-100 <= Node.val <= 100

代码

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
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: None Do not return anything, modify root in-place instead.
"""
if root == None:
return
self.flatten(root.left)
self.flatten(root.right)

right = root.right
# 将左子树替换掉右子树
root.right = root.left
root.left = None

# 找到右子树最右的节点,接上原右子树
p = root
while (p.right != None):
p = p.right
p.right = right
StriveZs wechat
Hobby lead  creation, technology change world.