使用match解构

This commit is contained in:
游由 2022-02-14 12:54:37 +08:00
parent 6be230c7c4
commit 252fa11ebb
6 changed files with 24 additions and 31 deletions

View File

@ -37,9 +37,8 @@ impl Solution {
/// * 👎 0
pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
match (p, q) {
(Some(l), Some(r)) => {
let (mut a, mut b) = (l.borrow_mut(), r.borrow_mut());
a.val == b.val && Self::is_same_tree(a.left.take(), b.left.take()) && Self::is_same_tree(a.right.take(), b.right.take())
(Some(l), Some(r)) => match (l.borrow_mut(), r.borrow_mut()) {
(mut a, mut b) => a.val == b.val && Self::is_same_tree(a.left.take(), b.left.take()) && Self::is_same_tree(a.right.take(), b.right.take())
}
(Some(_), None) => false,
(None, Some(_)) => false,

View File

@ -31,9 +31,8 @@ impl Solution {
/// * 👎 0
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
match root {
Some(node) => {
let mut p = node.borrow_mut();
if p.left.is_none() {
Some(node) => match node.borrow_mut() {
mut p => if p.left.is_none() {
Self::min_depth(p.right.take()) + 1
} else if p.right.is_none() {
Self::min_depth(p.left.take()) + 1

View File

@ -7,6 +7,7 @@ impl Solution {
/// [112.路径总和](https://leetcode-cn.com/problems/path-sum/)
///
/// 2022-02-10 17:27:58
///
/// 给你二叉树的根节点 `root` 和一个表示目标和的整数 `targetSum` 。判断该树中是否存在 **根节点到叶子节点** 的路径,这条路径上所有节点值相加等于目标和 `targetSum` 。如果存在,返回 `true` ;否则,返回 `false` 。
///
/// + **叶子节点** 是指没有子节点的节点。
@ -41,9 +42,8 @@ impl Solution {
/// * 👎 0
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
match root {
Some(node) => {
let mut p = node.borrow_mut();
(p.left.is_none() && p.right.is_none() && p.val == target_sum) || Self::has_path_sum(p.left.take(), target_sum - p.val) || Self::has_path_sum(p.right.take(), target_sum - p.val)
Some(node) => match node.borrow_mut() {
mut p => (p.left.is_none() && p.right.is_none() && p.val == target_sum) || Self::has_path_sum(p.left.take(), target_sum - p.val) || Self::has_path_sum(p.right.take(), target_sum - p.val)
}
_ => false
}

View File

@ -45,9 +45,8 @@ impl Solution {
/// * 👎 0
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
match root {
Some(node) => {
let mut p = node.borrow_mut();
Some(Rc::new(RefCell::new(TreeNode { val: p.val, left: Self::invert_tree(p.right.take()), right: Self::invert_tree(p.left.take()) })))
Some(node) => match node.borrow_mut() {
mut p => Some(Rc::new(RefCell::new(TreeNode { val: p.val, left: Self::invert_tree(p.right.take()), right: Self::invert_tree(p.left.take()) })))
}
_ => None
}

View File

@ -40,17 +40,16 @@ impl Solution {
/// 👍 724 👎 0
pub fn merge_trees(root1: Option<Rc<RefCell<TreeNode>>>, root2: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
match (root1, root2) {
(Some(t1), Some(t2)) => {
let (mut t1, mut t2) = (t1.borrow_mut(), t2.borrow_mut());
Some(Rc::new(RefCell::new(TreeNode {
(Some(t1), Some(t2)) => match (t1.borrow_mut(), t2.borrow_mut()) {
(mut t1, mut t2) => Some(Rc::new(RefCell::new(TreeNode {
val: t1.val + t2.val,
left: Solution::merge_trees(t1.left.take(), t2.left.take()),
right: Solution::merge_trees(t1.right.take(), t2.right.take()),
left: Self::merge_trees(t1.left.take(), t2.left.take()),
right: Self::merge_trees(t1.right.take(), t2.right.take()),
})))
}
(None, Some(t2)) => Some(t2),
(Some(t1), None) => Some(t1),
(None, None) => None
(None, None) => None,
(None, some) => some,
(some, None) => some
}
}
}

View File

@ -30,18 +30,15 @@ impl Solution {
///
/// 👍 178 👎 0
pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, low: i32, high: i32) -> i32 {
fn sum_range(root: Option<&Rc<RefCell<TreeNode>>>, low: i32, high: i32) -> i32 {
if let Some(r) = root {
let r = r.borrow();
if r.val > high {
return sum_range(r.left.as_ref(), low, high);
match root {
Some(node) => match node.borrow_mut() {
mut p => match p.val {
v if v > high => Self::range_sum_bst(p.left.take(), low, high),
v if v < low => Self::range_sum_bst(p.right.take(), low, high),
_ => p.val + Self::range_sum_bst(p.left.take(), low, high) + Self::range_sum_bst(p.right.take(), low, high)
}
if r.val < low {
return sum_range(r.right.as_ref(), low, high);
}
return r.val + sum_range(r.left.as_ref(), low, high) + sum_range(r.right.as_ref(), low, high);
} else { 0 }
}
_ => 0
}
sum_range(root.as_ref(), low, high)
}
}