ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

力扣 题目99- 验证二叉搜索树

2022-07-27 19:35:28  阅读:166  来源: 互联网

标签:力扣 ergodic right TreeNode val nullptr 二叉 99 root


题目

题解

力扣 题目98- 验证二叉搜索树中 我们知道了 中序遍历后的二叉搜索树 应该为递增 那么出错就应该是有部分递减 那么我们在98题的基础上 反向检测 保存减少数列的开头与结尾进行交换

代码

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 
 6 
 7 struct TreeNode {
 8     int val;
 9     TreeNode* left;
10     TreeNode* right;
11     TreeNode() : val(0), left(nullptr), right(nullptr) {}
12     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13     TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
14 };
15 //遍历
16 //即减少序列的第一个数与最后一个数交换
17 class Solution {
18 public:
19     void recoverTree(TreeNode* root) {
20         TreeNode* last = new TreeNode(INT_MAX);
21         TreeNode* max = new TreeNode(INT_MAX);
22         TreeNode* min = new TreeNode(INT_MAX);
23         stack<TreeNode*> ergodic;
24         while (root != nullptr || !ergodic.empty()) {
25             if (root != nullptr) {
26                 ergodic.push(root);
27                 root = root->left;
28             }
29             else
30             {
31                 if (ergodic.top()->val < last->val) {
32                     if (max->val == INT_MAX) {
33                         max = last;
34                     }
35                     min = ergodic.top();
36                 }
37                 last = ergodic.top();
38                 root = ergodic.top()->right;
39                 ergodic.pop();
40             }
41         }
42         swap(max->val, min->val);
43     }
44 
45     vector<int> inorderTraversal(TreeNode* root) {
46         vector<int> result;
47         stack<TreeNode*> ergodic;
48         while (root != nullptr || !ergodic.empty()) {
49             if (root != nullptr) {
50                 ergodic.push(root);
51                 root = root->left;
52             }
53             else
54             {
55                 result.push_back(ergodic.top()->val);
56                 root = ergodic.top()->right;
57                 ergodic.pop();
58             }
59         }
60         return result;
61     }
62 
63 };
64 
65 int main() {
66     Solution sol;
67     TreeNode* head2 = new TreeNode(2);
68     TreeNode* head3 = new TreeNode(3, nullptr, head2);
69     TreeNode* head1 = new TreeNode(1, head3, nullptr);
70     sol.recoverTree(head1);
71     vector<int> resultforeach = sol.inorderTraversal(head1);
72     for (int i = 0; i < resultforeach.size(); i++) {
73         cout << resultforeach[i] << " ";
74     }
75 
76 }
View Code

 

标签:力扣,ergodic,right,TreeNode,val,nullptr,二叉,99,root
来源: https://www.cnblogs.com/zx469321142/p/16525999.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有