具体思路:
直接遍历找pre->val=root->val的情况;
具体代码:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:void fun(TreeNode* root,TreeNode* &pre,TreeNode* &final,TreeNode* p){if(root==nullptr||final!=nullptr){return;}fun(root->left,pre,final,p);if(pre!=nullptr&&pre->val==p->val){final=root;}pre=root;fun(root->right,pre,final,p);}TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {TreeNode* pre=nullptr; TreeNode* ret=nullptr;fun(root,pre,ret,p);return ret; }
};