Merge pull request #2729 from eliozh/master

Update 0236.二叉树的最近公共祖先.md
This commit is contained in:
程序员Carl
2024-09-22 17:29:55 +08:00
committed by GitHub

View File

@ -454,7 +454,11 @@ impl Solution {
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
if root == p || root == q || root.is_none() {
if root.is_none() {
return root;
}
if Rc::ptr_eq(root.as_ref().unwrap(), p.as_ref().unwrap())
|| Rc::ptr_eq(root.as_ref().unwrap(), q.as_ref().unwrap()) {
return root;
}
let left = Self::lowest_common_ancestor(