Leetcode 之Binary Tree Postorder Traversal(47)
中序遍历二叉搜索树,得到的是一个有序的结果,找出其中逆序的地方就可以了。如果逆序的地方相邻,只需把逆序的相换即可;如果不相邻,则需要找到第二个逆序对的
第二个元素再做交换。
定义两个指针p和q来指定需要交换的元素,指针pre记录当前结点的前驱结点,用来判断是否逆序。
void recoverTree(TreeNode *root)
{
pre = p = q = nullptr;
dfs(root);
swap(p->val, q->val);
}
void dfs(TreeNode *root)
{
if (!root)return;
dfs(root->left);
if (pre != nullptr && pre->val > root->val)
{
if (p == nullptr)
{
p = pre;
q = root;
}
else
q = root;
}
pre = root;
dfs(root->right);
}
Leetcode 之Binary Tree Postorder Traversal(47)的更多相关文章
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- leetcode - [6]Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
随机推荐
- BZOJ4653 & 洛谷1712 & UOJ222:[NOI2016]区间——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4653 https://www.luogu.org/problemnew/show/P1712 ht ...
- AOJ 7.Redraiment猜想
Redraiment猜想 Description redraiment在家极度无聊,于是找了张纸开始统计素数的个数. 设函数f(n)返回从1->n之间素数的个数. redraiment发现: f ...
- bzoj1062【Noi2008】糖果雨
orz.....神tm数形结合题 题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1062 插入线段,删除线段,查询区间内线段个数,线段随时间往复 ...
- 2017-7-18-每日博客-关于Linux下的history的常用命令.doc
History history命令可以用来显示曾执行过的命令.执行过的命令默认存储在HOME目录中的.bash_history文件中,可以通过查看该文件来获取执行命令的历史记录.需要注意的是.bash ...
- mybatis中parameterType可以写的别名
mybatis中parameterType可以写的别名 https://blog.csdn.net/sdzhangshulong/article/details/51749807 _byte byte ...
- Star sky 二维前缀和
C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- springboot-为内置tomcat设置虚拟目录
需求 项目使用springboot开发,以jar包方式部署.项目中文件上传均保存到D判断下的upload目录下. 在浏览器中输入http://localhost:8080/upload/logo_1. ...
- frame外弹出,刷新父页面
//刷新父页面 function reflashParent() { var id = parent.tabbar.getActiveTab(); id = id.replace('tab','mai ...
- asyncio Lock,Queue
# # total = 0 # # async def add(): # #1. dosomething1 # #2. io操作 # # 1. dosomething3 # global total ...
- mysql数据库cmd直接登录
找到mysql的安装路径: 将该路径配置到环境变量中: win+R代开dos窗口:输入mysql -uroot -p回车,输入密码.