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/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
随机推荐
- sticky用法
可以用于滚动到一定距离以后让他实现定位效果. 比如滚动到50px的时候让导航栏固定定位. 用法:给最外层的div设置绝对定位 然后要固定的div设置position:sticky; top:0: 这样 ...
- 测开之路六十五:UI测试平台之js
//添加网址的函数,生成一个输入网址的标签,并且把标签append到id为cases下function browser() { var html = '\ <div class="ro ...
- 16/8/23_CSS自动换行
转载:http://blog.csdn.net/ye987987/article/details/8011875 自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺 ...
- MapReduce(3): Partitioner, Combiner and Shuffling
Partitioner: Partitioning and Combining take place between Map and Reduce phases. It is to club the ...
- C++ STL map容器值为指针时怎么释放内存
最近在使用STL中map时,遇到了一个问题,就是当map中值为指针对象时怎么释放内存? // 站点与TCP连接映射表 (key为ip_port_stationCode, value为 clientSo ...
- pgsql删除重复记录
如下: DELETE FROM categories a WHERE ( a.id, a.name, a.parent_id ) ) ) 关键点:oracle中有内部id为rowid, 在postgr ...
- linux配置mysql数据库远程连接失败
今天配置linux下mysql数据库可以远程访问的问题,百度这方面的资料有很多,但是方法都一样,都试过了却未能解决,记录一下 第一步:在/etc/mysql/my.cnf下找到bind-address ...
- Codeforces - 1176E - Cover it! - bfs
https://codeforc.es/contest/1176/problem/E 久了不写bfs了.一开始用dfs写,的确用dfs是很有问题的,一些奇怪的情况就会导致多染一些色. 注意无向图的边要 ...
- python写入mysql
import pymysql conn = pymysql.connect(host='192.168.70.129',port=3306,user='root',passwd='123456', ...
- 源码分析--HashMap(JDK1.8)
在JDK1.8中对HashMap的底层实现做了修改.本篇对HashMap源码从核心成员变量到常用方法进行分析. HashMap数据结构如下: 先看成员变量: 1.底层存放数据的是Node<K,V ...