LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树
897. Increasing Order Search Tree
题目描述
给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点。
LeetCode897. Increasing Order Search Tree
示例:
```
5
/ \
3 6
/ \ \
2 4 8
/ / \
1 7 9
```
输出: [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
```
提示:
2. 每个结点都有一个从 0 到 1000 范围内的唯一整数值。
Java 实现
TreeNode 类
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
中序遍历(inorder)
import java.util.LinkedList;
import java.util.List;
class Solution {
public TreeNode increasingBST(TreeNode root) {
if (root == null) {
return null;
}
List<Integer> res = new LinkedList<>();
inorder(root, res);
TreeNode ans = new TreeNode(0);
TreeNode cur = ans;
for (int val : res) {
cur.right = new TreeNode(val);
cur = cur.right;
}
return ans.right;
}
public void inorder(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
}
参考资料
- https://leetcode.com/problems/increasing-order-search-tree/
- https://leetcode-cn.com/problems/increasing-order-search-tree/
LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)的更多相关文章
- [Swift]LeetCode897. 递增顺序查找树 | 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 解题报告(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 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
题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为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 ...
- LeetCode897. 递增顺序查找树
题目 法一.自己 1 class Solution { 2 public: 3 vector<int>res; 4 TreeNode* increasingBST(TreeNode* ro ...
随机推荐
- Python 全栈开发【第0篇】:目录
Python 全栈开发[第0篇]:目录 第一阶段:Python 开发入门 Python 全栈开发[第一篇]:计算机原理&Linux系统入门 Python 全栈开发[第二篇]:Python基 ...
- shell history 命令
1.history命令可以显示历史执行过的命令: 2.使用!+序号执行该序号对应的命令: 例子 $ history sed 's/haha/hello/g' test cat test cat tes ...
- Tensorflow object detection API(1)---环境搭建与测试
参考: https://blog.csdn.net/dy_guox/article/details/79081499 https://blog.csdn.net/u010103202/article/ ...
- 范仁义html+css课程---2、html常用标签
范仁义html+css课程---2.html常用标签 一.总结 一句话总结: html常用的标签有 标题标签.div.span.p.hr.br标签 等 1.html中的标题标签有哪些? <h1& ...
- 7年.NET面试Java的尴尬历程
先简单介绍LZ 现如今的情况,LZ 1992年出生,2012年实习,大专学渣一枚,实习期直接被校企合作直招到公司做.NET开发,现如今在某三线城市做后端技术经理,7年开发经验(5年.Net,2年.NE ...
- Cesium 禁止相机进入地底下[转]
原文:https://blog.csdn.net/thor027/article/details/82455649 viewer.clock.onTick.addEventListener(funct ...
- vue子组件数据变化同步到父组件中
方法:通过watch监听子组件数据变化 1.父组件中注册方法 <Child @getChildValue="getChildValue"></Child> ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战
笔记 1.@SpringBootTest单元测试实战 简介:讲解SpringBoot的单元测试 1.引入相关依赖 <!--springboot程 ...
- npm使用国内源
npm使用国内源 转 https://www.jianshu.com/p/7e84d7b119bc $ npm install -g cnpm --registry=https://registr ...
- javascript取模运算是怎么算的?其实是取余数
问到是否整除,这里记录下取模 比如120分钟是不是整点?120%60 === 0 为整点 javascript取模运算是一个表达式的值除以另一个表达式的值,并返回余数. 取模在js里就是取余数的意思. ...