897. 递增顺序查找树

897. Increasing Order Search Tree

题目描述

给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点。

LeetCode897. Increasing Order Search Tree

示例:

输入: [5,3,6,2,4,null,8,1,null,null,null,7,9]
```
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
```

提示:

1. 给定树中的结点数介于 1 和 100 之间。
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);
}
}

参考资料

LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)的更多相关文章

  1. [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 ...

  2. 【Leetcode_easy】897. Increasing Order Search Tree

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

  3. 897. Increasing Order Search Tree

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

  4. 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...

  5. [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 ...

  6. 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 ...

  7. 【leetcode】897. Increasing Order Search Tree

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

  8. [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 ...

  9. LeetCode897. 递增顺序查找树

    题目 法一.自己 1 class Solution { 2 public: 3 vector<int>res; 4 TreeNode* increasingBST(TreeNode* ro ...

随机推荐

  1. MySQL UPDATE 更新

    以下是 UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法: UPDATE table_name SET field1=new-value1, field2=new-value2 [W ...

  2. Hadoop 压缩

    压缩的好处 文件压缩的好处:减少文件存储锁需要的磁盘空间,加速数据在网络和磁盘上的传输. 常见的压缩格式 压缩格式 工具 算法 文件扩展名 是否可以切分 DELATE 无 DEFLATE     .d ...

  3. [提权]mysql中的UDF提权

    由于udf提权是需要构造UDF函数文件的,涉及到了写文件.所以本次实验已经将mysql的配置做了改动:–secure-file-priv=''. 剧情须知: secure_file_priv 为 NU ...

  4. 开源GIT仓库-----gogs

    简介:Gogs 是一款极易搭建的自助 Git 服务,其目标是打造一个最简单.最快速和最轻松的方式搭建自助 Git 服务.使用 Go 语言开发使得 Gogs 能够通过独立的二进制分发,并且支持 Go 语 ...

  5. 学习DB2之Linux环境下下载

    一 下载前的注册IBM地址:https://www-01.ibm.com/marketing/iwm/iwm/web/pick.do?source=swg-db2expressc&S_PKG= ...

  6. 小福bbs-冲刺日志(第四天)

    [小福bbs-冲刺日志(第四天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 两个前端完成15个界面 作业的正文 小福bbs-冲刺日志( ...

  7. minicom调试4G网卡

    [root@localhost toybrick]# minicom -D /dev/ttyUSB2 Welcome to minicom 2.7.1                          ...

  8. R scholar和rentrez | NCBI和Google scholar文献数据挖掘

    主要会用到两个R包: rentrez: 'Entrez' in Rscholar: Analyse Citation Data from Google Scholar RISmed 包可以查询 Pub ...

  9. Tensorflow 2 flower_photos花卉数据集手动下载、离线安装、本地加载、快速读取

    Tensorflow 2 flower_photos花卉数据集手动下载.离线安装.本地加载.快速读取 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.c ...

  10. Vue动态路由 Get传值

    <template> <!-- 所有的内容要被根节点包含起来 --> <div id="home"> 我是首页组件 <ul> < ...