Skip to content

Commit 1fff326

Browse files
committed
'Balanced Binary Tree' soln
1 parent 90baa3f commit 1fff326

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ Note: Some solutions have multiple approaches implemented
115115
| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [Solution](./leetcode/binary_tree_level_order_traversal_ii.rs) | Medium |
116116
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Solution](./leetcode/convert_sorted_array_to_bst.rs) | Easy |
117117
| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Solution](./leetcode/convert_sorted_list_to_bst.rs) | Medium |
118-
| >110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Solution](./leetcode/balanced_binary_tree.rs) | Easy |
119-
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](./leetcode/minimum_depth_of_binary_tree.rs) | Easy |
118+
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Solution](./leetcode/balanced_binary_tree.rs) | Easy |
119+
| >111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](./leetcode/minimum_depth_of_binary_tree.rs) | Easy |
120120
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](./leetcode/path_sum.rs) | Easy |
121121
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](./leetcode/path_sum_ii.rs) | Medium |
122122
| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |

leetcode/balanced_binary_tree.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
///
2+
/// Problem: Balanced Binary Tree
3+
///
4+
/// Given a binary tree, determine if it is height-balanced.
5+
///
6+
/// A height-balanced binary tree is defined as a binary tree in which the depth of the
7+
/// two subtrees of every node never differ by more than 1.
8+
///
9+
/// Example 1:
10+
/// Input: root = [3,9,20,null,null,15,7]
11+
/// Output: true
12+
///
13+
/// Example 2:
14+
/// Input: root = [1,2,2,3,3,null,null,4,4]
15+
/// Output: false
16+
///
17+
/// Example 3:
18+
/// Input: root = []
19+
/// Output: true
20+
///
21+
/// Constraints:
22+
/// The number of nodes in the tree is in the range [0, 5000].
23+
/// -10^4 <= Node.val <= 10^4
24+
///
25+
26+
// Definition for a binary tree node (provided by LeetCode)
27+
// #[derive(Debug, PartialEq, Eq)]
28+
// pub struct TreeNode {
29+
// pub val: i32,
30+
// pub left: Option<Rc<RefCell<TreeNode>>>,
31+
// pub right: Option<Rc<RefCell<TreeNode>>>,
32+
// }
33+
//
34+
// impl TreeNode {
35+
// #[inline]
36+
// pub fn new(val: i32) -> Self {
37+
// TreeNode {
38+
// val,
39+
// left: None,
40+
// right: None
41+
// }
42+
// }
43+
// }
44+
45+
// # Solution:
46+
// Time complexity: O(n²)
47+
// Space complexity: O(n)
48+
49+
use std::rc::Rc;
50+
use std::cell::RefCell;
51+
use std::cmp;
52+
impl Solution {
53+
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
54+
if root.is_none() {
55+
return true;
56+
}
57+
58+
let node = root.unwrap();
59+
let node_ref = node.borrow();
60+
61+
// Get heights of left and right subtrees
62+
let left_height = Self::height(node_ref.left.clone());
63+
let right_height = Self::height(node_ref.right.clone());
64+
65+
// Check if current node is balanced and recursively check children
66+
(left_height - right_height).abs() <= 1
67+
&& Self::is_balanced(node_ref.left.clone())
68+
&& Self::is_balanced(node_ref.right.clone())
69+
}
70+
71+
72+
fn height(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
73+
if root.is_none() {
74+
return 0;
75+
}
76+
77+
let node = root.unwrap();
78+
let node_ref = node.borrow();
79+
80+
1 + cmp::max(
81+
Self::height(node_ref.left.clone()),
82+
Self::height(node_ref.right.clone())
83+
)
84+
}
85+
}

0 commit comments

Comments
 (0)