题目: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的更多相关文章

  1. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  2. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 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 解题报告

    题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 【leetcode】897. Increasing Order Search Tree

    题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...

  9. 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...

随机推荐

  1. install mysql firewall

  2. mysql_DCL_grant/revoke

    授权就是为某个用户赋予某些权限.例如,可以为新建的用户赋予查询所有数据库和表的权限.合理的授权能够保证数据库的安全.不合理的授权会使数据库存在安全隐患. MySQL中使用GRANT关键字来为用户设置权 ...

  3. java Json 技术记录

    1.Json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,包括commons-beanutils.jar,commons-c ...

  4. DEDECMS为文章添加NEW标志图片

    找到extend.func.php添加自定义函数: function shownewimg($pubtime){$ntime = time();$tagtime = $pubtime;$day3 = ...

  5. php 的定界符 <<<eof 的问题

    PHP是一个Web编程语言,在编程过程中难免会遇到用echo来输出大段的html和javascript脚本的情况,如果用传统的输出方法 ——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等特 ...

  6. 在reshard过程中,将会询问reshard多少slots:

    How many slots do you want to move (from 1 to 16384)?,取值范围为1~16384,其中16384为redis cluster的拥有的slots总数, ...

  7. 【五一qbxt】day6 OI中的stl

    from:why 很多很多part…… 1.pair: 相当于把两个变量放在一起: #include<utility> using namespace std; pair<TypeN ...

  8. java NIO socket 通信实例

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhuyijian135757/article/details/37672151 java Nio 通 ...

  9. 排序---快速排序及其切分函数Partition应用

    快速排序   快速排序通过一个切分元素将数组分成两个子数组,左子数组小于等于切分元素,右子数组大于切分元素,将这两个子数组排序,也就是将整个数组排序了. 代码如下: public class Sort ...

  10. Android禁止输入表情符号

    在我们Android开发的项目中,难免有要求在输入框中禁止输入表情,所以呢,写了一个输入框禁止输入表情的demo,供大伙参考 效果图 如图显示,如果用户输入了表情字符,会提示. EmojiFilter ...