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/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
随机推荐
- python脚本-上传apk至蒲公英
import requests import os #账号配置信息 url = "https://upload.pgyer.com/apiv1/app/upload" uKey = ...
- Myeclipse优化配置
#utf8 (do not remove)#utf8 (do not remove)-startup../Common/plugins/org.eclipse.equinox.launcher_1.2 ...
- swiper在vue中的用法
首先通过npm i vue-awesome-swiper --save 来在vue中下载插件 然后再main.js中引入 require('swiper/dist/css/swiper.css')im ...
- [转]Hook executed successfully but returned HTTP 403
原文地址:https://www.cnblogs.com/chenglc/p/11174530.html jenkins配置gitlab的webhook,完成配置,测试结果显示 Hook execut ...
- redis主从与集群搭建
redis搭建主从 条件:yum安装(3.2.1)与编译安装(5.0.0)都可以 环境:我这里在同一台主机上搭建,当然也可以两台. 1) 复制redis.conf的主配置文件并命令为slave.con ...
- Maven仓库存在jar包但依旧提示无法下载
介绍最近服务器的迁移,把原来服务器的地址都更改了,所以私服的地址也改动了,原来项目下载到本地仓库的包,但是重新构建过程中竟然发现依然要提示下载,本地仓库里面明明有包,为什么还要下载? 解决去maven ...
- package和import语句_4
J2SDK中主要的包介绍 java.lang—包含一些Java语言的核心类,如String.Math.Integer.System和 Thread,提供常用功能. java.awt—包含了构成抽象 ...
- spring-第十二篇之两种后处理器
1.扩展IoC容器使用后处理器扩展 bean后处理器:对容器中的bean进行后处理,也就是额外的加强. 容器后处理:对IoC容器进行后处理,增强容器功能. 2.bean后处理器 负责处理容器 ...
- JS事件循环,MACRO TASK,MICRO TASK
事件循环的基本概念 JS执行的过程中,由JS引擎控制的函数调用栈来控制时间循环 定时器线程,事件触发线程,异步http请求线程控制异步的任务队列 任务分为macro task,micro task 对 ...
- 小B的询问(题解)(莫队)
小B的询问(题解)(莫队) Junlier良心莫队 题目 luoguP2709 小B的询问 code #include<bits/stdc++.h> #define lst long lo ...