99. Recover Binary Search Tre
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
=============
解法:来自leetcode 150题集
O(n)的解法,开一个指针数组,中序遍历,将节点指针一次存放在数组中,
然后寻找两处逆向的位置,先从前往后找第一个逆序的位置,然后从后往前寻找第二个逆序的位置,交换两个指针的值,
递归/非递归中序遍历一般需要用到栈,空间也是O(n)的,可以利用morris中序遍历的方式
=====
code:
- /**
- * 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:
- ///
- void recoverTree(TreeNode *root){
- pair<TreeNode *,TreeNode *> broken;
- TreeNode *curr = root;
- TreeNode *prev = nullptr;
- broken.first = broken.second = nullptr;
- while(curr!=nullptr){
- if(curr->left==nullptr){
- detect(broken,prev,curr);
- prev = curr;
- curr = curr->right;
- }else{
- auto node = curr->left;
- ///prev = curr->left;
- while(node->right != nullptr && node->right!=curr){
- node = node->right;
- }
- ///find predecessor
- if(node->right==nullptr){
- node->right = curr;
- curr = curr->left;
- }else{
- node->right = nullptr;
- detect(broken,prev,curr);
- prev = curr;
- curr = curr->right;
- }
- }///if-else
- }///while
- swap(broken.first->val,broken.second->val);
- }
- void detect(pair<TreeNode *,TreeNode *> &broken,TreeNode *prev,
- TreeNode *curr){
- if(prev!=nullptr && prev->val > curr->val){
- if(broken.first == nullptr) broken.first = prev;
- broken.second = curr;
- }
- }
- };
99. Recover Binary Search Tre的更多相关文章
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 99. 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 ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 99. Recover Binary Search Tree
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- LeetCode OJ 99. 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...
随机推荐
- 让Grub 2来拯救你的 bootloader
没有什么事情比 bootloader 坏掉更气人的了,充分发挥 Grub 2 的作用,让 bootloader 安分工作吧.为什么这么说? Grub 2 是最受欢迎的 bootloader ,几乎用在 ...
- 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Sprint第二个冲刺(第六天)
一.Sprint 计划会议: 因为这两天课程较多的原因,表面上进度可能可能没有太大的变化,其实组员们都有完善之前做的功能,正在做的功能也在抓紧时间完成.俗话说得好,慢工出细活 ,不能因为赶进度就随便做 ...
- xctest错误问题解决
xctest xctest.h file not found(null): Framework not found XCTest 在FrameWork Search Path里增加以下内容$(PLAT ...
- HTML 常见代码整合;
html+css代码 文本设置 1.font-size: 字号参数 2.font-style: 字体格式 3.font-weight: 字体粗细 4.颜色属性 color: 参数 注意使用网页安全色 ...
- android解析json包(接口)
package com.http.test; 02 03 04 import org.apache.http.HttpResponse; 05 import org.apache.http ...
- poj2912 带权并查集
题意:多个人玩石头剪刀布,每个人提前选定了自己出哪个手势,而其中有一种特殊的人他可以随意出什么手势,问是否能够从给出的一系列石头剪刀布游戏中判断出哪个是特殊的,可以从第几局游戏中判断出来. 首先按照食 ...
- 越狱Season 1-Episode 8: The Old Head
Season 1, Episode 8: The Old Head -Michael: 17 days from now they strap my brother to an electric ch ...
- click 绑定(二)带参数的click 事件绑定
注1:传参数给你的click 句柄 最简单的办法是传一个function包装的匿名函数: <button data-bind="click: function() { viewMode ...
- php 代码大全
1.子类访问父类静态方法 <?php class A{ static function loadById(){ $class_name = get_called_class(); $model ...