leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0235.cpp (431B)


      1 class Solution {
      2   public:
      3     TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
      4         TreeNode *tmp = root;
      5         while (true) {
      6             if (root->val > p->val && root->val > q->val)
      7                 root = root->left;
      8             else if (root->val < p->val && root->val < q->val)
      9                 root = root->right;
     10             else
     11                 break;
     12         }
     13         return root;
     14     }
     15 };