Skip to content

Commit dbf8c8e

Browse files
committed
feat: add tree
1 parent c2a941d commit dbf8c8e

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"sliding_window",
99
"String",
1010
"greedy",
11+
"Tree",
1112
"trie",
1213
"stack",
1314
"Math",

Tree/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "leetcode_tree"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
path = "lib.rs"
9+
doctest = false
10+
11+
[dependencies]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// https://leetcode.com/problems/root-equals-sum-of-children
2+
//
3+
// You are given the `root` of a **binary tree** that consists of exactly `3` nodes: the root, its left child, and its right child.
4+
//
5+
// Return `true` _if the value of the root is equal to the **sum** of the values of its two children, or_ `false` _otherwise_.
6+
//
7+
// **Example 1:**
8+
//
9+
// ![](https://assets.leetcode.com/uploads/2022/04/08/graph3drawio.png)
10+
// ```
11+
// **Input:** root = [10,4,6]
12+
// **Output:** true
13+
// **Explanation:** The values of the root, its left child, and its right child are 10, 4, and 6, respectively.
14+
// 10 is equal to 4 + 6, so we return true.
15+
// ```
16+
//
17+
// **Example 2:**
18+
//
19+
// ![](https://assets.leetcode.com/uploads/2022/04/08/graph3drawio-1.png)
20+
// ```
21+
// **Input:** root = [5,3,1]
22+
// **Output:** false
23+
// **Explanation:** The values of the root, its left child, and its right child are 5, 3, and 1, respectively.
24+
// 5 is not equal to 3 + 1, so we return false.
25+
// ```
26+
//
27+
// **Constraints:**
28+
//
29+
// * The tree consists only of the root, its left child, and its right child.
30+
// * `-100 <= Node.val <= 100`
31+
32+
use std::cell::RefCell;
33+
use std::rc::Rc;
34+
use crate::base_tree::TreeNode;
35+
36+
pub fn check_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
37+
let root_node = root.as_ref().unwrap().borrow();
38+
let root_val = root.as_ref().unwrap().borrow().val;
39+
let left_val = root_node.left.as_ref().unwrap().borrow().val;
40+
let right_val = root_node.right.as_ref().unwrap().borrow().val;
41+
return root_val == left_val + right_val;
42+
}
43+
44+
#[test]
45+
pub fn t1() {
46+
let mut root = TreeNode::new(10);
47+
root.left = Some(Rc::new(RefCell::new(TreeNode::new(4))));
48+
root.right = Some(Rc::new(RefCell::new(TreeNode::new(6))));
49+
assert_eq!(check_tree(Some(Rc::new(RefCell::new(root)))), true);
50+
}

Tree/base_tree.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Definition for a binary tree node.
2+
3+
use std::cell::RefCell;
4+
use std::rc::Rc;
5+
6+
#[derive(Debug, PartialEq, Eq)]
7+
pub struct TreeNode {
8+
pub val: i32,
9+
pub left: Option<Rc<RefCell<TreeNode>>>,
10+
pub right: Option<Rc<RefCell<TreeNode>>>,
11+
}
12+
13+
impl TreeNode {
14+
#[inline]
15+
pub fn new(val: i32) -> Self {
16+
TreeNode {
17+
val,
18+
left: None,
19+
right: None
20+
}
21+
}
22+
}

Tree/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![allow(dead_code)]
2+
3+
mod _2236_root_equals_sum_of_children;
4+
mod base_tree;

0 commit comments

Comments
 (0)