作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/increasing-order-search-tree/description/

题目描述

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.

题目大意

把一棵树按照中序遍历的顺序重新安排,安排成最左侧的节点是新的数树的根节点,并且每个节点只有右子节点。

解题方法

重建二叉树

好久没做树的题目,有点生疏。使用的方式是最简单的,先中序遍历,得到顺序,然后再连接的方式。

这个做法的问题是用数组保存了整儿个中序遍历的值,然后重建了二叉树,那么空间复杂度挺大的,不是一个好方法。

时间复杂度是O(n),空间复杂度是O(n).

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def increasingBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
array = self.inOrder(root)
if not array:
return None
newRoot = TreeNode(array[0])
curr = newRoot
for i in range(1, len(array)):
curr.right =TreeNode(array[i])
curr = curr.right
return newRoot def inOrder(self, root):
if not root:
return []
array = []
array.extend(self.inOrder(root.left))
array.append(root.val)
array.extend(self.inOrder(root.right))
return array

数组保存节点

在上面解法的基础上,如果不想使用保存节点的值然后重新构建每个节点的方式,那么有个更简单的方法就是我们在数组里保存节点,然后直接把数组的节点再次构成树就好了。省去了重新构造每个节点的过程。

时间复杂度是O(n),空间复杂度是O(n).

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def increasingBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
res = self.inOrder(root)
if not res:
return
dummy = TreeNode(-1)
cur = dummy
for node in res:
node.left = node.right = None
cur.right = node
cur = cur.right
return dummy.right def inOrder(self, root):
if not root:
return []
res = []
res.extend(self.inOrder(root.left))
res.append(root)
res.extend(self.inOrder(root.right))
return res

中序遍历时修改指针

这个做法在上面的基础上再次缩减了空间复杂度,不再需要数组。这种做法中直接在中序遍历的过程中修改每个节点的指向。

修改指向的方式其实比较简单,使用prev指针一直指向了构造出来的这个新树的最右下边的节点,在中序遍历过程中把当前节点的左指针给设置为None,然后把当前节点放到新树的右下角,这样类似于一个越来越长的链表的构建过程。

时间复杂度是O(n),空间复杂度是O(1).

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def increasingBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
dummy = TreeNode(-1)
self.prev = dummy
self.inOrder(root)
return dummy.right def inOrder(self, root):
if not root:
return None
self.inOrder(root.left)
root.left = None
self.prev.right = root
self.prev = self.prev.right
self.inOrder(root.right)

参考资料

https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/

日期

2018 年 9 月 3 日 ———— 新学期开学第一天!
2018 年 11 月 1 日 —— 小光棍节

【LeetCode】897. Increasing Order Search Tree 解题报告(Python)的更多相关文章

  1. LeetCode 897 Increasing Order Search Tree 解题报告

    题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...

  2. [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  3. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  4. 897. Increasing Order Search Tree

    题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...

  5. 【leetcode】897. Increasing Order Search Tree

    题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...

  6. [LeetCode&Python] Problem 897. Increasing Order Search Tree

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  7. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...

  9. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. Python中pymysql基本使用

    Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作 优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚 缺点:操作繁琐,代码量多 1. pymy ...

  2. BAT的一些题

    114.java中实现多态的机制是什么 答:重写,重载.方法的重写Overriding和重载Overloading是Java多态性的不同表现.  重写Overriding是父类与子类之间多态性的一种表 ...

  3. CSS上下左右居中对齐

    上下左右居中对齐 display:  inline/inline-block 将父元素(容器)设定 text-align: center: 即可左右置中. display: block 将元素本身的 ...

  4. myatoi

    atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中.int atoi(const char *nptr) 函数会扫描参数 nptr字符串 ...

  5. C++ 继续(3n+1)猜想

    1005 继续(3n+1)猜想 (25分)   卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过 ...

  6. 数据库SQL性能优化

    1.in与exists的效率比较 in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询.一直以来认为exists 比in 效率高的说法是不准 ...

  7. 网页设计单位 px,em,rem,vm,vh,%

    px(pixels) 像素 (px) 是一种绝对单位,因为无论其他相关的设置怎么变化,像素指定的值是不会变化的. px就是设备或者图片最小的一个点,比如常常听到的电脑像素是1024x768的,表示的是 ...

  8. Mycat的事务异常:Caused by: java.sql.SQLException: Transaction error, need to rollback.Distributed transaction is disabled!

    工作中踩到的一个坑 ,一个报错,导致整个服务不能用.工程部署四个节点,请求是按轮询机制分发的,所以请求四次报错,整个系统瘫痪.记录下 . 项目环境:spring +Mybaties +mycat +D ...

  9. JDBCUtils工具类的属性

    package cn.itcast.util;import java.io.FileReader;import java.io.IOException;import java.net.URL;impo ...

  10. Redis5.0.8 Cluster集群部署

    目录 一.Redis Cluster简介 二.部署 三.创建主库 一.Redis Cluster简介 Redis Cluster集群是一种去中心化的高可用服务,其内置的sentinel功能可以提供高可 ...