/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @param TreeNode $p
* @param TreeNode $q
* @return TreeNode
*/
function lowestCommonAncestor($root, $p, $q) {
if (empty($root)) {
return $root;
}
if (($root->val <= $p->val && $root->val >= $q->val)
|| ($root->val >= $p->val && $root->val <= $q->val)) {
return $root;
}
if ($root->val >= $p->val && $root->val >= $q->val) {
return $this->lowestCommonAncestor($root->left, $p, $q);
}
if ($root->val <= $p->val && $root->val <= $q->val) {
return $this->lowestCommonAncestor($root->right, $p, $q);
}
}
}