博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
897.Increasing Order Search Tree
阅读量:5217 次
发布时间:2019-06-14

本文共 1476 字,大约阅读时间需要 4 分钟。

Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.

Example 1:

Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]

5      / \    3    6   / \    \  2   4    8 /        / \ 1        7   9

Output: [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

Note:

  1. The number of nodes in the given tree will be between 1 and 100.
  2. Each node will have a unique integer value from 0 to 1000.
# Definition for a binary tree node.class TreeNode:    def __init__(self, x):        self.val = x        self.left = None        self.right = Noneclass Solution:    def increasingBST(self, root):        """        :type root: TreeNode        :rtype: TreeNode        """        l = []        if root is None:            return None        def inorder(root):            if root==None:                return            if root.left:                inorder(root.left)            l.append(root.val)            if root.right:                inorder(root.right)        inorder(root = root)        newtree = TreeNode(l[0])        build = newtree        for i in l[1:]:            build.right = TreeNode(i)            build = build.right        return newtree

转载于:https://www.cnblogs.com/bernieloveslife/p/9756319.html

你可能感兴趣的文章
Android多线程分析之四:MessageQueue的实现
查看>>
codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
查看>>
Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
查看>>
linux set
查看>>
vim lua对齐indent无效
查看>>
Python中的MySQL接口:PyMySQL & MySQLdb
查看>>
输入格式CombineFileInput
查看>>
笔记:mysql 下载与安装
查看>>
CSS3学习笔记
查看>>
本地推送UILocalNotification
查看>>
在Android中调用C#写的WebService(附源代码)
查看>>
108. Convert Sorted Array to Binary Search Tree
查看>>
数据结构与算法
查看>>
红黑树
查看>>
DIV滚动条
查看>>
YOLOV3 训练WIDER_FACE
查看>>
GIS ftp
查看>>
精品课程-工程测量-第一章-绪论
查看>>
好久没写代码,今天刚刚给客户写了点
查看>>
C++之 类型定义语句--typedef
查看>>