为什么生锈可以直接使用==检查两棵树?
问题:检查两种二元树是否相同。
我的解决方案:使用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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用隐式递归DFS。您可以使用货物expand 或工具 - &gt;在操场上展开宏。
#[derive(partiaLeq)]
在您的示例输出中:这可能令人生畏,但实际上与以下内容相同:
因此,它首先比较该值,然后递归到
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:That might be daunting, but is actually the same as:
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.