【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
作者: 负雪明烛
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:
- The number of nodes in the given tree will be between 1 and 100.
- 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)的更多相关文章
- 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 ...
- [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 ...
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- 897. Increasing Order Search Tree
题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...
- 【leetcode】897. Increasing Order Search Tree
题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...
- [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 ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- 18-Rotate Array-Leetcode
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- 学习java 7.28
学习内容: Applet Applet一般称为小应用程序,Java Applet就是用Java语言编写的这样的一些小应用程序,它们可以通过嵌入到Web页面或者其他特定的容器中来运行,也可以通过Java ...
- 从源码看RequestMappingHandlerMapping的注册与发现
1.问题的产生 日常开发中,大多数的API层中@Controller注解和@RequestMapping注解都会被使用在其中,但是为什么标注了@Controller和@RequestMapping注解 ...
- deque、queue和stack深度探索(下)
deque如何模拟连续空间?通过源码可以看到这个模型就是通过迭代器来完成. 迭代器通过重载操作符+,-,++,--,*和->来实现deque连续的假象,如上图中的 finish-start ,它 ...
- Linux基础命令---mirror获取ftp目录
mirror 使用lftp登录ftp服务器之后,可以使用mirror指令从服务器获取目录 1.语法 mirror [OPTS] [source [target]] 2.选项列表 选 ...
- shell awk命令字符串拼接
本节内容:awk命令实现字符串的拼接 输入文件的内容: TMALL_INVENTORY_30_GROUP my163149.cm6 3506 5683506 mysql-bin.000013 3273 ...
- window安装ab压力测试
ab是Apache HTTP server benchmarking tool的缩写,可以用以测试HTTP请求的服务器性能,也是业界比较流行和简单易用的一种压力测试工具包 ## 下载 下载地址:(ht ...
- 解决在进行socket通信时,一端输出流OutputStream不关闭,另一端输入流就接收不到数据
输出的数据需要达到一定的量才会向另一端输出,所以在传输数据的末端添加 \r\n 可以保证不管数据量是多少,都立刻传输到另一端.
- 【C/C++】金币
做了一下去年的题目,今年看起来就没这么难了 从上到下的可以从下到上考虑,会简单很多,dp入门 题目 金币 小招在玩一款游戏,在一个N层高的金字塔上,以金字塔顶为第一层,第i层有i个落点,每个落点有若干 ...
- 利用代码覆盖率提高嵌入式软件的可靠性 - VectorCAST
简介 代码覆盖率是衡量软件测试完成情况的指标,通常基于测试过程中已检查的程序源代码比例 计算得出.代码覆盖率可以有效避免包含未测试代码的程序被发布. 代码覆盖率能不能提高软件的可靠性?答案是肯定的,代 ...