572. Subtree of Another Tree Easy

@problem@discussion
#Tree#Depth-First Search#String Matching#Binary Tree#Hash Function



1/**
2 * [572] Subtree of Another Tree
3 *
4 * Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.
5 * A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/subtree1-tree.jpg" style="width: 532px; height: 400px;" />
9 * Input: root = [3,4,5,1,2], subRoot = [4,1,2]
10 * Output: true
11 * 
12 * Example 2:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/subtree2-tree.jpg" style="width: 502px; height: 458px;" />
14 * Input: root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
15 * Output: false
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	The number of nodes in the root tree is in the range [1, 2000].
21 * 	The number of nodes in the subRoot tree is in the range [1, 1000].
22 * 	-10^4 <= root.val <= 10^4
23 * 	-10^4 <= subRoot.val <= 10^4
24 * 
25 */
26pub struct Solution {}
27use crate::util::tree::{TreeNode, to_tree};
28
29// problem: https://leetcode.com/problems/subtree-of-another-tree/
30// discuss: https://leetcode.com/problems/subtree-of-another-tree/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for a binary tree node.
35// #[derive(Debug, PartialEq, Eq)]
36// pub struct TreeNode {
37//   pub val: i32,
38//   pub left: Option<Rc<RefCell<TreeNode>>>,
39//   pub right: Option<Rc<RefCell<TreeNode>>>,
40// }
41// 
42// impl TreeNode {
43//   #[inline]
44//   pub fn new(val: i32) -> Self {
45//     TreeNode {
46//       val,
47//       left: None,
48//       right: None
49//     }
50//   }
51// }
52use std::rc::Rc;
53use std::cell::RefCell;
54impl Solution {
55    pub fn is_subtree(root: Option<Rc<RefCell<TreeNode>>>, sub_root: Option<Rc<RefCell<TreeNode>>>) -> bool {
56        false
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_572() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.