Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
- {
- if (root == NULL || p == NULL || q == NULL)
- return NULL;
- while ((root != NULL) && (root != p) && (root != q) && ((root->val > p->val && root->val > q->val) || (root->val < p->val && root->val < q->val)))
- {
- if (root->val > p->val)
- root = root->left;
- else
- root = root->right;
- }
- return root;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement