|
| 1 | +/// @number 100 |
| 2 | +/// @title Same Tree |
| 3 | +/// @url https://leetcode.com/problems/same-tree |
| 4 | +/// @difficulty easy |
| 5 | +
|
| 6 | +//Given two binary trees, write a function to check if they are the same or not. |
| 7 | +// |
| 8 | +// |
| 9 | +// Two binary trees are considered the same if they are structurally identical a |
| 10 | +//nd the nodes have the same value. |
| 11 | +// |
| 12 | +// Example 1: |
| 13 | +// |
| 14 | +// |
| 15 | +//Input: 1 1 |
| 16 | +// / \ / \ |
| 17 | +// 2 3 2 3 |
| 18 | +// |
| 19 | +// [1,2,3], [1,2,3] |
| 20 | +// |
| 21 | +//Output: true |
| 22 | +// |
| 23 | +// |
| 24 | +// Example 2: |
| 25 | +// |
| 26 | +// |
| 27 | +//Input: 1 1 |
| 28 | +// / \ |
| 29 | +// 2 2 |
| 30 | +// |
| 31 | +// [1,2], [1,null,2] |
| 32 | +// |
| 33 | +//Output: false |
| 34 | +// |
| 35 | +// |
| 36 | +// Example 3: |
| 37 | +// |
| 38 | +// |
| 39 | +//Input: 1 1 |
| 40 | +// / \ / \ |
| 41 | +// 2 1 1 2 |
| 42 | +// |
| 43 | +// [1,2,1], [1,1,2] |
| 44 | +// |
| 45 | +//Output: false |
| 46 | +// |
| 47 | +// Related Topics Tree Depth-first Search |
| 48 | +// 👍 2062 👎 59 |
| 49 | + |
| 50 | + |
| 51 | +struct Solution; |
| 52 | +#[derive(Debug, PartialEq, Eq)] |
| 53 | +pub struct TreeNode { |
| 54 | + pub val: i32, |
| 55 | + pub left: Option<Rc<RefCell<TreeNode>>>, |
| 56 | + pub right: Option<Rc<RefCell<TreeNode>>>, |
| 57 | +} |
| 58 | + |
| 59 | +impl TreeNode { |
| 60 | + #[inline] |
| 61 | + pub fn new(val: i32) -> Self { |
| 62 | + TreeNode { |
| 63 | + val, |
| 64 | + left: None, |
| 65 | + right: None |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +//leetcode submit region begin(Prohibit modification and deletion) |
| 70 | +// Definition for a binary tree node. |
| 71 | + |
| 72 | +use std::rc::Rc; |
| 73 | +use std::cell::RefCell; |
| 74 | +impl Solution { |
| 75 | + pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool { |
| 76 | + match (p, q) { |
| 77 | + (Some(p_node), Some(q_node)) => { |
| 78 | + let (t1, t2) = (p_node.borrow(), q_node.borrow()); |
| 79 | + let value_equals = t1.val == t2.val; |
| 80 | + |
| 81 | + let left_equals = Solution::is_same_tree(t1.left.clone(), t2.left.clone()); |
| 82 | + let right_equals = Solution::is_same_tree(t1.right.clone(), t2.right.clone()); |
| 83 | + value_equals && left_equals && right_equals |
| 84 | + }, |
| 85 | + (None, None) => true, |
| 86 | + _ => false |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +//leetcode submit region end(Prohibit modification and deletion) |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod test { |
| 97 | + use crate::same_tree::{Solution, TreeNode}; |
| 98 | + use std::rc::Rc; |
| 99 | + use std::cell::RefCell; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test() { |
| 103 | + let mut node = TreeNode::new(1); |
| 104 | + node.left = Some(Rc::new(RefCell::new(TreeNode::new(2)))); |
| 105 | + node.right = Some(Rc::new(RefCell::new(TreeNode::new(3)))); |
| 106 | + |
| 107 | + let mut node2 = TreeNode::new(1); |
| 108 | + node2.left = Some(Rc::new(RefCell::new(TreeNode::new(2)))); |
| 109 | + node2.right = Some(Rc::new(RefCell::new(TreeNode::new(3)))); |
| 110 | + |
| 111 | + assert_eq!(true, Solution::is_same_tree( |
| 112 | + Some(Rc::new(RefCell::new(node))), |
| 113 | + Some(Rc::new(RefCell::new(node2))), |
| 114 | + )); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test2() { |
| 119 | + let mut node = TreeNode::new(1); |
| 120 | + node.left = Some(Rc::new(RefCell::new(TreeNode::new(2)))); |
| 121 | + |
| 122 | + let mut node2 = TreeNode::new(1); |
| 123 | + node2.right = Some(Rc::new(RefCell::new(TreeNode::new(2)))); |
| 124 | + |
| 125 | + assert_eq!(false, Solution::is_same_tree( |
| 126 | + Some(Rc::new(RefCell::new(node))), |
| 127 | + Some(Rc::new(RefCell::new(node2))), |
| 128 | + )); |
| 129 | + } |
| 130 | + #[test] |
| 131 | + fn test3() { |
| 132 | + let mut node = TreeNode::new(1); |
| 133 | + node.left = Some(Rc::new(RefCell::new(TreeNode::new(2)))); |
| 134 | + node.right = Some(Rc::new(RefCell::new(TreeNode::new(1)))); |
| 135 | + |
| 136 | + let mut node2 = TreeNode::new(1); |
| 137 | + node2.left = Some(Rc::new(RefCell::new(TreeNode::new(1)))); |
| 138 | + node2.right = Some(Rc::new(RefCell::new(TreeNode::new(2)))); |
| 139 | + |
| 140 | + assert_eq!(false, Solution::is_same_tree( |
| 141 | + Some(Rc::new(RefCell::new(node))), |
| 142 | + Some(Rc::new(RefCell::new(node2))), |
| 143 | + )); |
| 144 | + } |
| 145 | +} |
0 commit comments