lc235 Lowest Common Ancestor of a Binary Search Tree 發表於 2016-10-30 | 分類於 leetcode | 0 comments | 因為是BST直接搜索p,q直到分叉或者碰到p/q,第一個開始分叉的節點,或先碰到的p/q是LCA。 1234567891011121314151617181920/** * 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { int m = min(p -> val, q -> val); int n = max(p -> val, q -> val); if (!root || (root->val > m && root->val < n) || root->val == m || root->val == n) return root; else if (root->val < m) return lowestCommonAncestor(root->right, p, q); else return lowestCommonAncestor(root->left, p, q); } };