/**
* 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:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
vector<TreeNode*> ret;
if(root==NULL)return NULL;
inorderHelper(ret,root);
for(int i=0;i<ret.size()-1;i++){
if(ret[i]==p)return ret[i+1];
}
return NULL;
}
void inorderHelper(vector<TreeNode*>& ret,TreeNode* root)
{
if(root==NULL)return;
inorderHelper(ret,root->left);
ret.push_back(root);
inorderHelper(ret,root->right);
}
};
中序遍历打印即可