题解 | #找到搜索二叉树中两个错误的节点#
找到搜索二叉树中两个错误的节点
http://www.nowcoder.com/practice/4582efa5ffe949cc80c136eeb78795d6
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root * @return int整型vector */ vector<int> res{-1, -1}; TreeNode* pre = NULL; void helper(TreeNode* root){ if(root == NULL) return; helper(root -> left); if(pre != NULL && pre -> val > root -> val && res[1] == -1) res[1] = pre -> val; if(pre != NULL && pre -> val > root -> val && res[1] != -1) res[0] = root -> val; pre = root; helper(root -> right); } vector<int> findError(TreeNode* root) { // write code here helper(root); return res; } };