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 of the tree, and every node has no left child and only 1 right child.
题目分析及思路
题目给出一棵树,要求重新对树进行排序,使得树中最左边的结点是树的根,并且每一个结点没有左孩子,只有一个右孩子。可以先得到已知树的中序遍历,再依次创建新的结点并让每一个结点只有一个右孩子。
python代码
# 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':
def inorder(root):
order = []
if not root:
return order
order.extend(inorder(root.left))
order.append(root.val)
order.extend(inorder(root.right))
return order
in_order = inorder(root)
nodes = []
for v in in_order:
nodes.append(TreeNode(v))
for i, v in enumerate(nodes):
if (i+1) == len(in_order):
break
else:
v.right = nodes[i+1]
return nodes[0]
LeetCode 897 Increasing Order Search Tree 解题报告的更多相关文章
- 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
- [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 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 ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
随机推荐
- elk问题,求教各位大虾!
[filebeat --> kafka --> logstash-->MongoDB|磁盘]架构进行日志收集 但是当logstash写入MongoDB有延迟,然后正常之后,会导致lo ...
- Android开发(十五)——ListView中Items的间距margin
ListView中Items没有margin 参考:http://www.cnblogs.com/xitang/p/3677528.html
- 开发过程中遇到的问题1--------我们的mysql的查询语句时自己写的,没有用oracle的nextvalue函数。所以这里涉及到了并发的问题。
效果http://www.cnblogs.com/wanggangblog/p/4037543.html 很多的采购单会生成.生成的时候会有订单的编号,然后一个采购单的编号是唯一的,怎么生成呢?之前o ...
- win7怎么打开加锁文件夹
在windows 7下,C:\Documents and Settings文件夹是默认锁定的,很多时候要进入进行解除锁定,根据本人实践的经历略作介绍. 1.打计算机,进入c盘文件夹,2.分别点击“组织 ...
- 【python】关键网站
https://pypi.org https://www.python.org/search/?q=pyhanlp&submit= https://www.lfd.uci.edu/~gohlk ...
- Kubernetes部署SpringCloud(二) 部署ZUUL与服务 非host, 伸缩与负载均衡
因为服务需要可缩容,所以不能使用host部署. 涉及两个应用,zuul,basic-info-api 验证,在k8s任意一个node 从zuul 访问 basic-info-api 创建一个Sprin ...
- STL中的map、unordered_map、hash_map
转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...
- linux-Centos 7下tftp-server服务的安装与配置
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间 进行简单文件传输的协议,提供不复杂.开销不大的文件传输服 ...
- spring的一些其他功能
今天看书,知识点多一点了,涉及到了较多的知识,如scheduling计划任务,spring对多线程的支持,以及配置开发.测试和生产环境的profile.这些都是集成在spring中的,需要用时可以参考 ...
- J - S-Nim
Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as ...