给你一个树,请你 按中序遍历 重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点。
示例 :
输入:[5,3,6,2,4,null,8,1,null,null,null,7,9]
5
/ \
3 6
/ \ \
2 4 8
/ / \
1 7 9
输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
\
9
提示:
- 给定树中的结点数介于
1和100之间。 - 每个结点都有一个从
0到1000范围内的唯一整数值。
**难度**: Easy
**标签**: 树、 深度优先搜索、
# -*- coding: utf-8 -*-
# @Author : LG
"""
执行用时:24 ms, 在所有 Python3 提交中击败了99.87% 的用户
内存消耗:13.4 MB, 在所有 Python3 提交中击败了65.60% 的用户
解题思路:
递归
先中序遍历,然后组成数
"""
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def increasingBST(self, root: TreeNode) -> TreeNode:
result = []
def find(root):
if root:
find(root.left)
result.append(root.val)
find(root.right)
find(root)
# 建树
start = now = TreeNode(0)
for r in result:
now.right = TreeNode(r)
now = now.right
return start.right
