博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Recover Binary Search Tree
阅读量:5773 次
发布时间:2019-06-18

本文共 2692 字,大约阅读时间需要 8 分钟。

As the note in the problem statement, this problem has a straight-forward O(n)-space solution, which is to generate the inorder traversal results of the bst and then find the two nodes that violate the increasing trend and those are the two that requires to be swapped.

utilizes inorder traversal to find those two nodes without keeping all the results. However, inorder traversal implemented recursively will take at least O(logn) space and may even take O(n) space at the worst case. The code is rewritten as follows.

1 class Solution { 2 public: 3     void recoverTree(TreeNode* root) { 4         pre = first = second = NULL; 5         inorder(root); 6         if (first) { 7             int temp = first -> val; 8             first -> val = second -> val; 9             second -> val = temp;10         }11     }12 private:13     TreeNode *first, *second, *pre;14     void inorder(TreeNode* root) {15         if (!root) return;16         inorder(root -> left);17         if (pre && pre -> val > root -> val) {18             if (!first) first = pre;19             second = root;20         }21         pre = root;22         inorder(root -> right);23     }24 };

So to come up with an O(1)-space solution, we indeed have to turn to Morris traversal. has a nice explanation of how Morris traversal can be modified to find the two wrong nodes.

1 class Solution { 2 public: 3     void recoverTree(TreeNode* root) { 4         TreeNode* cur = root; 5         TreeNode *first, *second, *pre; 6         first = second = pre = NULL; 7         while (cur) { 8             if (cur -> left) { 9                 TreeNode* predecessor = cur -> left;10                 while (predecessor -> right && predecessor -> right != cur)11                     predecessor = predecessor -> right;12                 if (!(predecessor -> right)) {13                     predecessor -> right = cur;14                     cur = cur -> left;15                 }16                 else {17                     predecessor -> right = NULL;18                     if (pre && pre -> val > cur -> val) {19                         if (!first) first = pre;20                         second = cur;21                     }22                     pre = cur;23                     cur = cur -> right;24                 }25             }26             else {27                 if (pre && pre -> val > cur -> val) {28                     if (!first) first = pre;29                     second = cur;30                 }31                 pre = cur;32                 cur = cur -> right;33             }34         }35         if (first) swap(first -> val, second -> val);36     }37 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4649828.html

你可能感兴趣的文章
Resume简历中装B的词汇总结大全
查看>>
python udp编程实例
查看>>
TortoiseSVN中图标的含义
查看>>
js原生继承之——构造函数式继承实例
查看>>
linux定时任务的设置
查看>>
[CareerCup] 13.3 Virtual Functions 虚函数
查看>>
[Angular 2] ng-model and ng-for with Select and Option elements
查看>>
Visio中如何让重叠图形都显示
查看>>
Tasks and Back stack 详解
查看>>
关于EXPORT_SYMBOL的作用浅析
查看>>
成功的背后!(给所有IT人)
查看>>
在SpringMVC利用MockMvc进行单元测试
查看>>
Nagios监控生产环境redis群集服务战
查看>>
Angular - -ngKeydown/ngKeypress/ngKeyup 键盘事件和鼠标事件
查看>>
Android BlueDroid(一):BlueDroid概述
查看>>
Java利用httpasyncclient进行异步HTTP请求
查看>>
循环多少次? 【杭电--HDOJ-1799】 附题+具体解释
查看>>
linux系统终端命令提示符设置(PS1)记录
查看>>
C++运算符重载
查看>>
【Web】URI和URL,及URL的编码
查看>>