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. [转] Hadoop入门系列(一)Window环境下搭建hadoop和hdfs的基本操作

    转自:https://blog.csdn.net/qq_32938169/article/details/80209083 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载 ...

  2. Python自动生成代码工具

    项目中有一个需求,对一个基类而言,拥有一个比较方法和拷贝方法,某些地方需要频繁地对这两个方法进行调用.对于所有子类而言,需要重写这两个方法,并在其中维护类内一些成员变量.例如有一个变量m_iMyVal ...

  3. (二)SQL学习之数据定义类SQL

    以mysql为例 对数据库的常用操作 创建数据库:CREATE DATABASE mydb; 删除数据库:DROP DATABASE mydb; 切换数据库:USE mydb; 查询数据库:SHOW ...

  4. 机器不学习:CNN入门讲解-为什么要有最后一层全连接

    哈哈哈,又到了讲段子的时间 准备好了吗? 今天要说的是CNN最后一层了,CNN入门就要讲完啦..... 先来一段官方的语言介绍全连接层(Fully Connected Layer) 全连接层常简称为 ...

  5. windows下CEF3的关闭流程《转》

    原文地址:https://github.com/fanfeilong/cefutil/blob/master/doc/CEF_Close.md ============================ ...

  6. 微信小程序左滑删除未操作有复位效果

    1.wxml <!--pages/test/test.wxml--> <view class="page"> <movable-area class= ...

  7. mycat启动报Unable to start JVM: No such file or directory (2)【转】

    mycat启动失败,查看日志 /mycat/logs/wrapper.log发现如下信息 1  STATUS | wrapper  | 2017/11/22 16:15:17 | --> Wra ...

  8. 【java/Json】用Java对象构建Json语法树

    本文后续:https://www.cnblogs.com/xiandedanteng/p/11973129.html 编译第一步:将文本解析成Java对象构成的语法树 第二步:将语法树输出整形好的Js ...

  9. MiniUI表单验证实践

    学习实践: <form id="form2"> <div id="update_pas" style="width:380px&qu ...

  10. python接入微博第三方API之1环境准备

    环境准备: 1.注册微博账号 2.注册应用