|
| 1 | +## :pencil2:Leetcode之PHP版题目解析(237. Delete Node in a Linked List) |
| 2 | +**2020-04-12 吴亲库里 库里的深夜食堂** |
| 3 | +**** |
| 4 | +### :pencil2:描述 |
| 5 | +**给定一棵二叉查找树和两个节点,求出这两个节点最近的公共祖先(一个节点也可以是它自己的祖先)** |
| 6 | +**** |
| 7 | +### :pencil2:题目实例 |
| 8 | +<a href="https://github.com/wuqinqiang/"> |
| 9 | + <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/235.png"> |
| 10 | +</a> |
| 11 | + |
| 12 | +**** |
| 13 | + |
| 14 | +### :pencil2:题目分析 |
| 15 | +**两个节点出现的位置无非有三种情况:** |
| 16 | +> 一个在左子树一个右子树(可能不是一个层级的节点) |
| 17 | +
|
| 18 | +> 全在左子树 |
| 19 | +
|
| 20 | +> 全在右子树 |
| 21 | +
|
| 22 | +<a href="https://github.com/wuqinqiang/"> |
| 23 | + <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/235-1.png"> |
| 24 | +</a> |
| 25 | + |
| 26 | +**假设现在在节点2这个位置,给定的p,q节点分别是3,5,这时候判断发现3,5 都大于2,此时我们知道他的公共祖先肯定出现在右子树(包括有可能是2),但是不能确定,所以就需要进入下一层,反之,在左子树上也是同样的道理** |
| 27 | + |
| 28 | + |
| 29 | +### :pencil2:递归实现 |
| 30 | + |
| 31 | + |
| 32 | +```php |
| 33 | + /** |
| 34 | + * Definition for a binary tree node. |
| 35 | + * class TreeNode { |
| 36 | + * public $val = null; |
| 37 | + * public $left = null; |
| 38 | + * public $right = null; |
| 39 | + * function __construct($value) { $this->val = $value; } |
| 40 | + * } |
| 41 | + */ |
| 42 | + |
| 43 | + class Solution { |
| 44 | + /** |
| 45 | + * @param TreeNode $root |
| 46 | + * @param TreeNode $p |
| 47 | + * @param TreeNode $q |
| 48 | + * @return TreeNode |
| 49 | + */ |
| 50 | + function lowestCommonAncestor($root, $p, $q) { |
| 51 | + $parentVal=$root->val; |
| 52 | + $pVal=$p->val; |
| 53 | + $qVal=$q->val; |
| 54 | + if($pVal>$parentVal && $qVal>$parentVal){ |
| 55 | + return $this->lowestCommonAncestor($root->right,$p,$q); |
| 56 | + }else if($pVal<$parentVal && $qVal<$parentVal){ |
| 57 | + return $this->lowestCommonAncestor($root->left,$p,$q); |
| 58 | + }else{ |
| 59 | + return $root; |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + } |
| 64 | +``` |
| 65 | + |
| 66 | +### :pencil2:迭代实现 |
| 67 | + |
| 68 | + |
| 69 | +```php |
| 70 | + /** |
| 71 | + * @param TreeNode $root |
| 72 | + * @param TreeNode $p |
| 73 | + * @param TreeNode $q |
| 74 | + * @return TreeNode |
| 75 | + */ |
| 76 | + function lowestCommonAncestor($root, $p, $q) { |
| 77 | + $pVal=$p->val; |
| 78 | + $qVal=$q->val; |
| 79 | + $temp=$root; |
| 80 | + while($temp !=null){ |
| 81 | + if($pVal > $temp->val && $qVal>$temp->val){ |
| 82 | + $temp=$temp->right; |
| 83 | + }else if ($pVal<$temp->val && $qVal < $temp->val){ |
| 84 | + $temp=$temp->left; |
| 85 | + }else{ |
| 86 | + return $temp; |
| 87 | + } |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
| 91 | +``` |
| 92 | +**** |
| 93 | + |
| 94 | +### 联系 |
| 95 | + |
| 96 | +<a href="https://github.com/wuqinqiang/"> |
| 97 | + <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px"> |
| 98 | +</a> |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
0 commit comments