【LeetCode 99】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
题意:
一颗二叉搜索树中有2个结点的元素被误换了,要求恢复二叉搜索树的原状。
思路:
中序遍历BST,若发现一次逆序,说明是两个相连结点的误换,直接交换结点的值;若发现两次逆序,则为不相连的两个结点误换,交换错误的结点即可。用一个变量保存先前结点的状态,若发生逆序则记录,空间复杂度为常量。
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: vector<TreeNode* > errs;
TreeNode *preNode; void rec(TreeNode *root)
{
if(root->left != )
rec(root->left); if(preNode != && root->val < preNode->val)
{
errs.push_back(preNode);
errs.push_back(root);
} preNode = root; if(root->right != )
rec(root->right);
} void recoverTree(TreeNode* root) {
if(root == )
return ; preNode = ; rec(root); int temp = , index = ; if(errs.size() == )
index = ; temp = errs[]->val;
errs[]->val = errs[index]->val;
errs[index]->val = temp;
}
};
【LeetCode 99】Recover Binary Search Tree的更多相关文章
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【leetcode刷题笔记】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
- LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode具体分析 :: Recover Binary Search Tree [Tree]
Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straigh ...
随机推荐
- 学习selenium需要具备的知识或技术
因为有QTP和JAVA的基础,selenium学起来会快很多,总结下学习selenium所需要具备的知识或技术 1.selenium进行的自动化测试是基于ui层面的,所以html,css,javasc ...
- linux 命令小例
xargs示例: ls |xargs -i mv {} /opt find示例: find -mtime +n -name “*.avi” -type f -exec rm {} \; find - ...
- DP:LCS(最长公共子串、最长公共子序列)
1. 两者区别 约定:在本文中用 LCStr 表示最长公共子串(Longest Common Substring),LCSeq 表示最长公共子序列(Longest Common Subsequence ...
- *[hackerrank]Sam and sub-strings
https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings DP.注意到以N[i]结尾的每个字符串的子数字和有规律: 53 ...
- TCL语言笔记:TCL练习二
一.练习 1.二进制转十进制 proc b2d {b} { ;set len [string length $b] } {$i<$len} {incr i} { incr sum [expr , ...
- Java学习笔记之:Java数组
一.介绍 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如number ...
- 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器
基于HTML5和CSS的焦点图特效,梅花图案的背景很有中国特色,而且还会动哦,效果超炫,推荐下载! 演示图 html代码 <!DOCTYPE html PUBLIC "-//W3C// ...
- python mysql 简单总结(MySQLdb模块 需另外下载)
python 通过DB-API规范了它所支持的不同的数据库,使得不同的数据库可以使用统一的接口来访问和操作. 满足DB-API规范的的模块必须提供以下属性: 属性名 描述 apilevel DB-AP ...
- linux快速修改文件夹及文件下所有文件与文件夹权限
分两部分改属主和权限: 更改权限,递归方式 chmod -R 755 /var/www/html/test.com 更改属主,递归 chown -R apache:apache /var/www/ht ...
- 课程设计之(struts2+Hibernate)航空订票系统
1.题目 课程设计之航空订票系统 为某家机票预订服务商开发一个机票预订和查询管理系统.该系统中的航班和机票信息由多家航空公司负责提供.客户通过上网方式查询航班时间表.机票可用信息.机票折扣信息,可以远 ...