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 ...
随机推荐
- BZOJ5343 & 洛谷4602 & LOJ2555:[CTSC2018]混合果汁——题解
https://www.luogu.org/problemnew/show/P4602 https://loj.ac/problem/2555 https://www.lydsy.com/JudgeO ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- 【套题】qbxt国庆刷题班D1
Day1 事实上D1的题目还是比较简单的= =然而D1T2爆炸了就十分尴尬--错失一波键盘 看题 T1 传送门 Description 现在你手里有一个计算器,上面显示了一个数\(S\),这个计算器十 ...
- ACE中UDP通信
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585205.html udp是一种无连接的协议,提供无连接不可靠的服务. 在ace中,通 ...
- Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...
- poj2060——Taxi Cab Scheme(最小路径覆盖)
Description Running a taxi station is not all that simple. Apart from the obvious demand for a centr ...
- spring cloud config的bootstrap.yml与application.proterties的区别
bootstrap.yml 和application.yml 都可以用来配置参数 bootstrap.yml可以理解成系统级别的一些参数配置,这些参数一般是不会变动的 application.ym ...
- Android数据库资料
一.联系人和通话记录: 数据库文件/data/data/com.android.providers.contacts/databases/contacts2.db 通话记录的数据存在calls表中; ...
- saltstack:multi-master configuration
官方手册地址:http://docs.saltstack.com/topics/tutorials/multimaster.html 总结起来,有以下几步: Create a redundant ma ...
- bzoj 1135 [POI2009]Lyz 线段树+hall定理
1135: [POI2009]Lyz Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 573 Solved: 280[Submit][Status][ ...