Leetcode_897. Increasing Order Search Tree
题目:https://leetcode.com/problems/increasing-order-search-tree/
题意:
将一棵二叉搜索树,重排为一棵递增的二叉排序树。
解法1:
rson->root->left的顺序依次遍历整棵树,途中使用头插法建立链表(递增的二叉搜索树)。
这样做会带来额外的内存开销。
class Solution
{
public:
TreeNode* result=NULL;
TreeNode* increasingBST(TreeNode* root)
{ reverse_postorder(root);
return result;
} void reverse_postorder(TreeNode* root)
{
if(root==nullptr)
return;
reverse_postorder(root->right);
TreeNode* node=new TreeNode(root->val);
node->right=result;
result=node;
reverse_postorder(root->left);
}
};
解法2:
先序遍历树,途中调整指针的指向。
successor:以当前root为根节点的子树,在整棵树的先序表示中的后继。
在先序表示中,
任意节点(node),是以该节点左孩子(node->left)为根节点的子树的后继;
任意节点(node)的父节点,是以该节点的右孩子(node->right)为根节点的子树的后继;
class Solution
{
public:
TreeNode* increasingBST(TreeNode* root, TreeNode* successor=nullptr)
{
if(root==nullptr)
return successor;
TreeNode* res=increasingBST(root->left, root);
root->left=nullptr;
root->right=increasingBST(root->right, successor);
return res;
}
};
Leetcode_897. Increasing Order Search Tree的更多相关文章
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 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 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- [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&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 递增顺序查找树
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
题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...
- 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
随机推荐
- Node.js - 使用 Express 和 http-proxy 进行反向代理
安装 Express 和 http-proxy npm install --save express http-proxy 反向代理代码 proxy.js var express = require( ...
- PHP 调试 - 方式
之前学 Java 的时候,一直使用 IDE 的 console 控制台进行调试.后来搞 PHP 后,习惯在代码里面 echo 和 exit,然后在浏览器刷新看效果,把单步调试.变量值查看等常用的调试方 ...
- Vagrant 手册之 Vagrantfile - 提示及技巧
原文地址 Vagrantfile 是一种非常灵活的配置格式.语法基于 Ruby,可以用它做很多事情.在本页使用一些提示和技巧时,请注意正确使用它们. 1. 使用循环定义虚拟机 如果你想对多机器应用稍微 ...
- mooc-IDEA 调试代码--012
mooc-IDEA 调试代码 添加断点快捷键:ctrl+F8 单步运行:F9 <=>resum(从一个断点跳转到下一个断点) 一行一行运行:F8 查看所有断点: 禁止所有断点: 条件断点 ...
- Js类的静态方法与实例方法区分以及jQuery如何拓展两种方法
上学时C#老师讲到对象有两类方法,静态方法(Static)和实例方法(非Static),当时不理解静态是为何意,只是强记. 后来从事前端工作,一直在对类(即对象,Js中严格来说没有类的定义,虽众所周知 ...
- SQL常用语句之数据库的创建、删除以及属性的修改-篇幅1
本篇文章主要总结了SQL Server 语句的使用和一些基础知识,因为目前我也正在学习,所以总结一下. 要使用数据库语句,首先就要知道数据库对象的结构: 通常情况下,如果不会引起混淆,可以直接使用对象 ...
- 解决Linux下Svn检出Windows SVN服务器上项目SSL handshake failed: SSL error: Key usage violation in certificate has been detected.
在Linux上检出windows SVN服务器上项目时出现了SSL handshake failed: SSL error: Key usage violation in certificate ha ...
- Asp.Netcore使用Filter来实现接口的全局异常拦截,以及前置拦截和后置拦截
原文链接:https://blog.csdn.net/qq_38762313/article/details/85234594 全局异常拦截器: 解决写每个接口都需要去做容错而添加try{ ...
- elasticsearch 基础 —— 请求体查询
请求体查询 简易 查询 -query-string search- 对于用命令行进行即席查询(ad-hoc)是非常有用的. 然而,为了充分利用查询的强大功能,你应该使用 请求体 search API, ...
- linux系统部署war包,查看tomcat日志
1.部署war包app/tomcat/bin在tomcat/bin 目录下启动 .startup.sh,在启动过程中tomcat会对war包进行解压,形成相应的项目目录 执行命令:./startup. ...