|
| 1 | +#![allow(dead_code)] |
| 2 | +use std::rc::Rc; |
| 3 | +use std::cell::RefCell; |
| 4 | + |
| 5 | +#[derive(Debug, PartialEq, Eq)] |
| 6 | +pub struct TreeNode { |
| 7 | + pub val: i32, |
| 8 | + pub left: Option<Rc<RefCell<TreeNode>>>, |
| 9 | + pub right: Option<Rc<RefCell<TreeNode>>>, |
| 10 | +} |
| 11 | + |
| 12 | +impl TreeNode { |
| 13 | + #[inline] |
| 14 | + pub fn new(val: i32) -> Self { |
| 15 | + TreeNode { |
| 16 | + val, |
| 17 | + left: None, |
| 18 | + right: None |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool { |
| 24 | + if root.is_none() { |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + let left_height = height(root.as_ref().unwrap().borrow().left.clone()); |
| 29 | + let right_height = height(root.as_ref().unwrap().borrow().right.clone()); |
| 30 | + |
| 31 | + if (left_height - right_height).abs() > 1 { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + return is_balanced(root.as_ref().unwrap().borrow().left.clone()) && is_balanced(root.as_ref().unwrap().borrow().right.clone()); |
| 36 | +} |
| 37 | + |
| 38 | +fn height(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { |
| 39 | + if root.is_none() { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + let left_height = height(root.as_ref().unwrap().borrow().left.clone()); |
| 44 | + let right_height = height(root.as_ref().unwrap().borrow().right.clone()); |
| 45 | + |
| 46 | + return 1 + std::cmp::max(left_height, right_height); |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | + Algorithm Name: Recursive DFS (Depth First Search) |
| 51 | + 1. If the root is None, return true |
| 52 | + 2. Get the height of the left subtree |
| 53 | + 3. Get the height of the right subtree |
| 54 | + 4. If the difference between the left and right subtree is greater than 1, return false |
| 55 | + 5. Recursively call is_balanced on the left and right subtree |
| 56 | + 6. Return the result of the recursive calls |
| 57 | +
|
| 58 | + Time complexity: O(n) |
| 59 | + Space complexity: O(n) |
| 60 | + */ |
| 61 | + |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_110() { |
| 69 | + let mut t1 = TreeNode::new(3); |
| 70 | + let mut t2 = TreeNode::new(9); |
| 71 | + let t3 = TreeNode::new(20); |
| 72 | + let t4 = TreeNode::new(15); |
| 73 | + let t5 = TreeNode::new(7); |
| 74 | + t2.left = Some(Rc::new(RefCell::new(t4))); |
| 75 | + t2.right = Some(Rc::new(RefCell::new(t5))); |
| 76 | + t1.left = Some(Rc::new(RefCell::new(t2))); |
| 77 | + t1.right = Some(Rc::new(RefCell::new(t3))); |
| 78 | + assert_eq!(is_balanced(Some(Rc::new(RefCell::new(t1)))), true); |
| 79 | + |
| 80 | + let mut t1 = TreeNode::new(1); |
| 81 | + let mut t2 = TreeNode::new(2); |
| 82 | + let mut t3 = TreeNode::new(2); |
| 83 | + let mut t4 = TreeNode::new(3); |
| 84 | + let mut t5 = TreeNode::new(3); |
| 85 | + let mut t6 = TreeNode::new(4); |
| 86 | + let t7 = TreeNode::new(4); |
| 87 | + t6.left = Some(Rc::new(RefCell::new(t7))); |
| 88 | + t5.left = Some(Rc::new(RefCell::new(t6))); |
| 89 | + t4.left = Some(Rc::new(RefCell::new(t5))); |
| 90 | + t3.left = Some(Rc::new(RefCell::new(t4))); |
| 91 | + t2.left = Some(Rc::new(RefCell::new(t3))); |
| 92 | + t1.left = Some(Rc::new(RefCell::new(t2))); |
| 93 | + assert_eq!(is_balanced(Some(Rc::new(RefCell::new(t1)))), false); |
| 94 | + } |
| 95 | +} |
0 commit comments