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. 《挑战30天C++入门极限》C++的iostream标准库介绍(2)

        C++的iostream标准库介绍(2) 接下来我们继续看一下C++风格的串流控制,C++引入了ostringstream.istringstream.stringstream这三个类,要使用 ...

  2. JS-选项卡制作解释部分

    <!DOCTYPE html> <html> <head> <meta name="author" content "郭菊锋,7 ...

  3. Leetcode84. 柱状图中最大的矩形(单调栈)

    84. 柱状图中最大的矩形 前置 单调栈 做法 连续区间组成的矩形,是看最短的那一块,求出每一块左边第一个小于其高度的位置,右边也同理,此块作为最短限制.需要两次单调栈 单调栈维护递增区间,每次不满足 ...

  4. docker安装postgresql

    1.在linux执行以下代码: docker run -p : -v /home/docker/postgresql/data:/var/lib/postgresql/data -e POSTGRES ...

  5. Tkinter 之磁盘搜索工具实战

    一.效果图 二.代码 miniSearch.py from tkinter import * from tkinter import ttk, messagebox, filedialog from ...

  6. Kafka - 环境搭建

    一.概述 Kafka(官网地址)专为分布式高吞吐量系统而设计. Kafka往往工作得很好,作为一个更传统的消息代理的替代品. 与其他消息传递系统相比,Kafka具有更好的吞吐量,内置分区,复制和固有的 ...

  7. python No module named 'urlparse'

    python3中,取消了urlparse 引用方式改为了: from urllib import parse

  8. Java查询目录下的所有文件(包括子目录)

    目录图: 方法代码: /** * 读取目录下的所有文件 * * @param dir * 目录 * @param fileNames * 保存文件名的集合 * @return */ public st ...

  9. linux中截取字段与#、$区别

    1.Linux shell 截取字符变量的前8位 实现方法有如下几种: expr substr “$a” 1 8 echo $a|awk ‘{print substr(,1,8)}’ echo $a| ...

  10. 解决request.getSession().getServletContext().getRealPath("/")为null问题

    今天把程序部署到服务器,发现异常,FileNotFound异常,很快定位到getServletContext().getRealPath("/");返回空的问题.这个问题通常是传递 ...