Advertisement
knakul853

Untitled

Jul 24th, 2020
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4.  
  5. class Solution {
  6. public:
  7.     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  8.        
  9.         if( !root) return NULL;
  10.        
  11.         int val = root->val;
  12.        
  13.         if(val > p->val && val > q->val)
  14.             return lowestCommonAncestor(root->left, p,q);
  15.              
  16.         if(val < p->val && val< q->val)
  17.             return lowestCommonAncestor(root->right, p,q);
  18.        
  19.         return root;
  20.        
  21.        
  22.     }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement