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/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
随机推荐
- SQLiteDatabase 数据库使用
0 SQLiteDatabases数据库特点 一种切入式关系型数据库,支持事务,可使用SQL语言,独立,无需服务.程序内通过类名可访问数据库,程序外不可以访问. SQLiteDatabases数据库使 ...
- UGUI OnValueChanged 动态参数指定
在选择方法的时候注意,选择最上面的动态参数的方法.
- Delphi Canvas的FillRect(const Rect: TRect) 函数的作用
http://blog.163.com/zhangzhifeng688@126/blog/static/165262758201131211341460/ Delphi Canvas的FillRect ...
- LATERAL VIEW 语法
LATERAL VIEW 使用语法 原文链接: https://www.deeplearn.me/2892.html select a.id, b.son_order_path from f_jz_c ...
- Jmeter接口测试报告模板优化
优化后在接口报告的接口信息中,直接展示url,method,结果和响应时间,详情中展示请求和响应数据.具体如下: 模板文件 jmeter-results-detail-report_21.xsl: & ...
- PHP Yii框架中使用smarty模板
第一种方法 按照YII系统的办法生成视图觉得有点麻烦,觉得用smarty更省事.尝试着把smarty模板加进来了. date_default_timezone_set("PRC") ...
- python实现压缩文件成zip格式
实现代码如下: #压缩文件 import time,zipfile class zip: def get_zip(self,files,zip_name): zp=zipfile.ZipFile(zi ...
- phpstudy开启PHPSocket扩展(windows系统)
PHP开启Socket扩展 一.windows系统(本地电脑) 1.打开phpstudy,设置——>配置文件——>打开php.ini(我安装的是PhpStudy v8.0,其他版本请自己找 ...
- Ride to Office(贪心水题)
[题目链接] http://noi.openjudge.cn/ch0406/2404/ [算法] 一开始zz了,先按时间排序然后如果速度超过当前男主速度,且在男主到达目的地前超过男主则最终男主和这个人 ...
- C# IEnumerable与IQueryable ,IEnumerable与IList ,LINQ理解Var和IEnumerable
原文:https://www.cnblogs.com/WinHEC/articles/understanding-var-and-ienumerable-with-linq.html 使用LINQ从数 ...