-
-
Save TakefumiYamamura/51870a171de967fefd47ec42a9dd9fca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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* target_successor; | |
TreeNode* target; | |
bool findTargetFlag; | |
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { | |
findTargetFlag = false; | |
target = p; | |
recursive(root); | |
return target_successor; | |
} | |
void recursive(TreeNode* cur) { | |
if(cur == NULL) return; | |
recursive(cur->left); | |
if(findTargetFlag && target_successor == NULL) { | |
target_successor = cur; | |
} | |
if(cur->val == target->val) findTargetFlag = true; | |
recursive(cur->right); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment