NC58. 找到搜索二叉树中两个错误的节点
描述
示例1
输入:
{1,2,3}
输出:
[1,2]
说明:
如题面图示例2
输入:
{4,2,5,3,1}
输出:
[1,3]
C++ 解法, 执行用时: 2ms, 内存消耗: 344KB, 提交时间: 2020-12-20
class Solution { public: /** * * @param root TreeNode类 the root * @return int整型vector */ vector<int> findError(TreeNode *root) { // write code herev vector<int> res, tmp; dfs(root, res); for (int i = 0; i < res.size() - 1; i++) { if (res[i] > res[i + 1]) { tmp.push_back(res[i]); int j = i + 1; while (j < res.size() && res[j] < res[i]) j++; tmp.push_back(res[j-1]); break; } } sort(tmp.begin(), tmp.end()); return tmp; } void dfs(TreeNode *root, vector<int> &v) { if (root == nullptr) return; dfs(root->left, v); v.push_back(root->val); dfs(root->right, v); } };
C++ 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2021-05-31
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root * @return int整型vector */ vector<int> findError(TreeNode* root) { // write code here vector<int> v; if(!root) return v; stack<TreeNode*> s; int prev = INT_MIN, nei; TreeNode* n = root; while(!s.empty() || n){ while(n){ s.push(n); n = n->left; } n = s.top(); s.pop(); if(n->val < prev){ if(v.size() == 0) { v.push_back(prev); nei = n->val; } else v.push_back(n->val); } prev = n->val; n = n->right; } if(v.size() == 1) v.push_back(nei); return vector<int>(v.rbegin(), v.rend()); } };
C++ 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2021-04-25
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root * @return int整型vector */ int cur = INT_MIN; vector<int> findError(TreeNode* root) { // write code here vector<int> res(2); find(root, res); return res; } void find(TreeNode* root, vector<int>& res) { if(!root) return; find(root->left, res); if(cur > root->val) { res[0] = root->val; res[1] = cur; } else { cur = root->val; } find(root->right, res); } };
C++ 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2021-01-31
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root * @return int整型vector */ vector<int> findError(TreeNode* root) { // write code here vector<int>result; if(root==nullptr) return result; result.resize(2); TreeNode* cur=root; stack<TreeNode*>nodes; vector<int>nums; while(nodes.size()||cur) { if(cur) { nodes.push(cur); cur=cur->left; }else { cur=nodes.top(); nodes.pop(); nums.push_back(cur->val); cur = cur->right; } } for(int i=0;i<nums.size()-1;++i) if(nums[i]>nums[i+1]) { result[1]=nums[i]; break; } for(int i=nums.size()-1;i>0;--i) if(nums[i]<nums[i-1]) { result[0]=nums[i]; break; } return result; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-05-09
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root * @return int整型vector */ vector<int> vec; TreeNode* pre=nullptr; int index=1; void inOrder(TreeNode* root){ if(root==nullptr) return; inOrder(root->left); if(pre!=nullptr){ if(index==1&&root->val<pre->val){ //找第一个数 要判断其小于之前的数 存pre vec[index]=pre->val; index--; } if(index==0&&root->val<pre->val) vec[index]=root->val; } pre=root;//初始指向第一个 inOrder(root->right); } vector<int> findError(TreeNode* root) { // write code here vec.resize(2); if(root==nullptr) return vec; inOrder(root); return vec; } };