1 //比较巧妙的解决方法,copied from:
 2 //http://blog.csdn.net/jellyyin/article/details/9018803
 3 class Solution {
 4 public:
 5     void recoverTree(TreeNode *root) {
 6         // Start typing your C/C++ solution below
 7         // DO NOT write int main() function
 8         TreeNode *n1=NULL;
 9         TreeNode *n2=NULL;
10         TreeNode *prev=NULL;
11         findTwoNodes(root,n1,n2,prev);
12
13         if(n1!=NULL && n2!=NULL)
14         {
15             int tmp=n2->val;
16             n2->val=n1->val;
17             n1->val=tmp;
18         }
19     }
20     void findTwoNodes(TreeNode *root, TreeNode *&n1, TreeNode *&n2, TreeNode *&prev)
21     {
22         if(root==NULL) return;
23
24         findTwoNodes(root->left,n1,n2,prev);
25         if(prev!=NULL && prev->val > root->val)
26         {
27             n2=root;
28             if(n1==NULL)
29             {
30                 n1=prev;
31             }
32         }
33         prev=root;
34         findTwoNodes(root->right,n1,n2,prev);
35     }
36 };

转载于:https://www.cnblogs.com/zhanghs/p/3475719.html

leetcode - Recover Binary Search Tree相关推荐

  1. [leetcode] Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  2. LeetCode 99. Recover Binary Search Tree

    LeetCode 99. Recover Binary Search Tree 刚看到这个题真是一脸懵逼啊... 博客转载自:http://www.cnblogs.com/grandyang/p/42 ...

  3. LeetCode: 173. Binary Search Tree Iterator

    LeetCode: 173. Binary Search Tree Iterator 题目描述 Implement an iterator over a binary search tree (BST ...

  4. LeetCode OJ - Recover Binary Search Tree

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  5. Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. 99. Recover Binary Search Tree (Tree; DFS)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. Recover Binary Search Tree,恢复二叉排序树

    问题描述:题意就是二叉树中有两个节点交换了,恢复结构. Two elements of a binary search tree (BST) are swapped by mistake. Recov ...

  8. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  9. LeetCode:Validate Binary Search Tree

    题目链接 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  10. 第五周 Leetcode 99. Recover Binary Search Tree (HARD)

    Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...

最新文章

  1. FaaS、PaaS和无服务器体系结构的优势
  2. Mongo DB 2.6 需要知道的一些自身限定
  3. lua的closure创建和使用
  4. 分布式版本控制系统Git的安装与使用
  5. linux下VMware_Tools虚拟机工具的安装
  6. QML工作笔记-使用QML中的Date将时间戳和指定格式时间互转
  7. 【U3D】掉落物设计
  8. c语言中栈的作用,栈(Stack)的概念和应用及C语言实现
  9. php多线程操作同一文件
  10. mmap文件做成html,mmap()映射到文件的一些总结
  11. COM组件的编写与注册
  12. 微信小程序弹框(后面有灰色蒙版) 获取手机号
  13. 水库河道应急广播系统解决方案
  14. 计算机制作情景短剧实验总结,校园情景剧 5分钟正能量情景剧剧本三篇 3分钟情景短剧剧本.docx...
  15. python之个性二维码制作
  16. ARM主板跑java_在ARM Cortex-M3上编写一个简单的C任意代码执行漏洞?
  17. 放弃北上广的N个理由
  18. Atlas:超精简WIN10系统
  19. 8月11_day08_日志框架基础
  20. sp3485电路设计

热门文章

  1. java中调用dos_JAVA如何调用DOS命令
  2. 一文读懂python数据分析_一文读懂聚类算法
  3. [渝粤教育] 西南科技大学 试验统计方法 在线考试复习资料2021版
  4. [渝粤教育] 西南科技大学 操作系统 在线考试复习资料
  5. ACM 学习笔记(七) 贪心
  6. stdlib.h函数请单
  7. ruminations on c++ 沉思录 源代码
  8. Building libmesh with Petsc support
  9. [转] crontab命令
  10. Google Translation Via Python