Update 0236.二叉树的最近公共祖先.md

优化Rust版本0236.二叉树的最近公共祖先代码
This commit is contained in:
Elio Zhou
2024-09-11 18:56:36 +08:00
committed by GitHub
parent dc2edca6ab
commit b5e3b801d3

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(