为什么生锈可以直接使用==检查两棵树?

发布于 2025-02-10 23:16:40 字数 1000 浏览 0 评论 0 原文

问题:检查两种二元树是否相同。

我的解决方案:使用DFS。

但是

在此解决方案中,

// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
// 
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
    pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>,
                        q: Option<Rc<RefCell<TreeNode>>>) -> bool {
        p == q
    }
}

生锈如何产生像这样的eq?

Question: check whether the two binary trees are the same.

My solution: use DFS.

But

https://leetcode.com/problems/same-tree/discuss/301998/Rust-One-Line-Solution

In this solution

// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
// 
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
    pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>,
                        q: Option<Rc<RefCell<TreeNode>>>) -> bool {
        p == q
    }
}

how does rust generate the Eq like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2025-02-17 23:16:40

使用隐式递归DFS。您可以使用货物expand 或工具 - &gt;在操场上展开宏。 #[derive(partiaLeq)] 在您的示例输出中:

impl ::core::marker::StructuralPartialEq for TreeNode {}
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::cmp::PartialEq for TreeNode {
    #[inline]
    fn eq(&self, other: &TreeNode) -> bool {
        match *other {
            Self {
                val: ref __self_1_0,
                left: ref __self_1_1,
                right: ref __self_1_2,
            } => match *self {
                Self {
                    val: ref __self_0_0,
                    left: ref __self_0_1,
                    right: ref __self_0_2,
                } => {
                    (*__self_0_0) == (*__self_1_0)
                        && (*__self_0_1) == (*__self_1_1)
                        && (*__self_0_2) == (*__self_1_2)
                }
            },
        }
    }
    #[inline]
    fn ne(&self, other: &TreeNode) -> bool {
        match *other {
            Self {
                val: ref __self_1_0,
                left: ref __self_1_1,
                right: ref __self_1_2,
            } => match *self {
                Self {
                    val: ref __self_0_0,
                    left: ref __self_0_1,
                    right: ref __self_0_2,
                } => {
                    (*__self_0_0) != (*__self_1_0)
                        || (*__self_0_1) != (*__self_1_1)
                        || (*__self_0_2) != (*__self_1_2)
                }
            },
        }
    }
}

这可能令人生畏,但实际上与以下内容相同:

impl PartialEq for TreeNode {
    fn eq(&self, other: &TreeNode) -> bool {
        self.val == other.val && self.left == other.left && self.right == other.right
    }
}

因此,它首先比较该值,然后递归到 left ,它将再次比较其价值...直到我们完成左边并从权利开始。 DFS。它可能与您手中写的内容不同,因为它是递归的并且可能会炸毁堆栈,但仍然有效。

Using implicit recursive DFS. You can observe the result of macros (including #[derive()] macros) using cargo-expand, or Tools->Expand Macros in the playground. The #[derive(PartialEq)] in your example outputs:

impl ::core::marker::StructuralPartialEq for TreeNode {}
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::cmp::PartialEq for TreeNode {
    #[inline]
    fn eq(&self, other: &TreeNode) -> bool {
        match *other {
            Self {
                val: ref __self_1_0,
                left: ref __self_1_1,
                right: ref __self_1_2,
            } => match *self {
                Self {
                    val: ref __self_0_0,
                    left: ref __self_0_1,
                    right: ref __self_0_2,
                } => {
                    (*__self_0_0) == (*__self_1_0)
                        && (*__self_0_1) == (*__self_1_1)
                        && (*__self_0_2) == (*__self_1_2)
                }
            },
        }
    }
    #[inline]
    fn ne(&self, other: &TreeNode) -> bool {
        match *other {
            Self {
                val: ref __self_1_0,
                left: ref __self_1_1,
                right: ref __self_1_2,
            } => match *self {
                Self {
                    val: ref __self_0_0,
                    left: ref __self_0_1,
                    right: ref __self_0_2,
                } => {
                    (*__self_0_0) != (*__self_1_0)
                        || (*__self_0_1) != (*__self_1_1)
                        || (*__self_0_2) != (*__self_1_2)
                }
            },
        }
    }
}

That might be daunting, but is actually the same as:

impl PartialEq for TreeNode {
    fn eq(&self, other: &TreeNode) -> bool {
        self.val == other.val && self.left == other.left && self.right == other.right
    }
}

So it first compares the value, and then recurses into left, which will again compare its value... until we finish the left and start with the rights. A DFS. It may not be the same as what you wrote in hand because it is recursive and may blow the stack, but it still works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文