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 ...
随机推荐
- C++:重载全局new/delete实现跨平台多线程内存检测
Reference: https://blog.csdn.net/u014023615/article/details/39551191 Reference: https://blog.csdn.ne ...
- masscan
masscan是一个快速的端口扫描工具 大概说一下它的使用方法,既有原创也有翻译 欢迎补充 扫描10.x.x.x的网络:masscan 10.0.0.0/8 -p80 程序将自动探测网络的接口和适配器 ...
- 非root用户如何使用docker命令
docker命令默认只能root使用的,但我们可以赋权给其他用户,使用时先照常新建用户: [root@10 ~]# useradd docker [root@10 ~]# passwd docker ...
- android( java) 处理 null 和 预防空指针异常(NullPointerException) 的一些经验。
概述: 在实际编码中总是会遇到 空指针异常 ,本文总结了一些处理空指针的个人经验. 原则: 尽早的检查,尽早的失败. 比如: 通过intent传参到新的目标 activity,而且一定需要这个参数,那 ...
- React Native 进的第一个坑
Bundling index.ios.js [development, non-minified, hmr disabled] 0.0% (0/1), failed. error: bundling ...
- 开源分布式日志系统ExceptionLess部署杂乱笔记 加密
前两天看到了这篇文章,亲身体会了下,确实不错,按照官方的文档试了试本地部署,折腾一番后终于成功,记下心得在此,不敢独享. 本地部署官方wiki .NET 4.6.1 这个因为我装了VS2015,就没有 ...
- [UI] 01 - CSS
前言 一.认识 From: http://www.runoob.com/css/css-tutorial.html CSS 指层叠样式表 (Cascading Style Sheets) 解决内容与表 ...
- 电子书阅读(epub) --- calibre
https://calibre-ebook.com/download calibre https://www.fosshub.com/Calibre.html win 64 bit calib ...
- Orleans学习总结(五)--监控篇
上篇说完了Orleans学习总结(四)--集群配置篇,这次我们来说下监控 Orleans有一个强大的社区,为Orleans开发着各种各样的扩展工具,我们用的是OrleansDashboard.Dash ...
- word2016怎么让目录索引显示在左边?
视图里面 勾选导航窗格即可 前提是你分级分好