|
| 1 | +// 701. Insert into a Binary Search Tree |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/insert-into-a-binary-search-tree/ |
| 5 | +// |
| 6 | +// Tags: Tree - Binary Search Tree - Binary Tree |
| 7 | + |
| 8 | +use std::cell::RefCell; |
| 9 | +use std::rc::Rc; |
| 10 | + |
| 11 | +struct Solution; |
| 12 | +impl Solution { |
| 13 | + // Travel through the tree choosing to go right or left based on node |
| 14 | + // values until we find the leave below which we will need to insert |
| 15 | + // the new node. |
| 16 | + // |
| 17 | + // Time Complexity: Best: O(log(n)) Worst: O(n) - We will travel the |
| 18 | + // entire height of the tree, ideally, it would be a well balanced tree |
| 19 | + // and its height would be log(n), but a skewed tree could have a height |
| 20 | + // of n, since we are not doing any balancing, after multiple inserts, |
| 21 | + // depending on the order in which the values were inserted, the tree |
| 22 | + // could become skewed. |
| 23 | + // Space complexity: O(1) - We only store a reference to the root and to |
| 24 | + // the current node that we are visiting. |
| 25 | + // |
| 26 | + // Runtime 12 ms Beats 72.73% |
| 27 | + // Memory 2.6 MB Beats 72.73% |
| 28 | + pub fn insert_into_bst( |
| 29 | + root: Option<Rc<RefCell<TreeNode>>>, |
| 30 | + val: i32, |
| 31 | + ) -> Option<Rc<RefCell<TreeNode>>> { |
| 32 | + let new_node = TreeNode { |
| 33 | + val, |
| 34 | + left: None, |
| 35 | + right: None, |
| 36 | + }; |
| 37 | + if root.is_none() { |
| 38 | + return Some(Rc::new(RefCell::new(new_node))); |
| 39 | + } |
| 40 | + let mut n = root.clone(); |
| 41 | + loop { |
| 42 | + match n { |
| 43 | + Some(node) => { |
| 44 | + if val < node.borrow().val { |
| 45 | + if node.borrow().left.is_some() { |
| 46 | + n = node.borrow().left.clone(); |
| 47 | + } else { |
| 48 | + node.borrow_mut().left = Some(Rc::new(RefCell::new(new_node))); |
| 49 | + break; |
| 50 | + } |
| 51 | + } else { |
| 52 | + if node.borrow().right.is_some() { |
| 53 | + n = node.borrow().right.clone(); |
| 54 | + } else { |
| 55 | + node.borrow_mut().right = Some(Rc::new(RefCell::new(new_node))); |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + None => panic!("This code should never run"), |
| 61 | + } |
| 62 | + } |
| 63 | + root |
| 64 | + } |
| 65 | + |
| 66 | + // Travel through the tree choosing to go right or left based on node |
| 67 | + // values until we find the leave below which we will need to insert |
| 68 | + // the new node. |
| 69 | + // |
| 70 | + // Time Complexity: Best: O(log(n)) Worst: O(n) - We will travel the |
| 71 | + // entire height of the tree, ideally, it would be a well balanced tree |
| 72 | + // and its height would be log(n), but a skewed tree could have a height |
| 73 | + // of n, since we are not doing any balancing, after multiple inserts, |
| 74 | + // depending on the order in which the values were inserted, the tree |
| 75 | + // could become skewed. |
| 76 | + // Space complexity: Best: O(log(n)) Worst: O(n) - Each node that we |
| 77 | + // visit will add one call to the call stack. |
| 78 | + // |
| 79 | + // Runtime 8 ms Beats 90.91% |
| 80 | + // Memory 2.6 MB Beats 72.73% |
| 81 | + pub fn insert_into_bst_rec( |
| 82 | + root: Option<Rc<RefCell<TreeNode>>>, |
| 83 | + val: i32, |
| 84 | + ) -> Option<Rc<RefCell<TreeNode>>> { |
| 85 | + // Always return a node reference, never None. |
| 86 | + Some(match root { |
| 87 | + // We are at the insertion point, create and return a new node. |
| 88 | + None => Rc::new(RefCell::new(TreeNode { |
| 89 | + val, |
| 90 | + left: None, |
| 91 | + right: None, |
| 92 | + })), |
| 93 | + // We are traveling down the tree decide whether to go right or left, |
| 94 | + // create a new node with the result of calling insert_into_bst on the |
| 95 | + // child and update the current child with that result. |
| 96 | + Some(node_ref) => { |
| 97 | + if val < node_ref.borrow().val { |
| 98 | + let node = Self::insert_into_bst(node_ref.borrow().left.clone(), val); |
| 99 | + node_ref.borrow_mut().left = node; |
| 100 | + } else { |
| 101 | + let node = Self::insert_into_bst(node_ref.borrow().right.clone(), val); |
| 102 | + node_ref.borrow_mut().right = node; |
| 103 | + } |
| 104 | + node_ref |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Tests. |
| 111 | +fn main() { |
| 112 | + // assert_eq!(Solution::min_diff_in_bst(root), 1); |
| 113 | + println!("All tests passed!") |
| 114 | +} |
| 115 | + |
| 116 | +// Definition for a binary tree node. |
| 117 | +#[derive(Debug, PartialEq, Eq)] |
| 118 | +pub struct TreeNode { |
| 119 | + pub val: i32, |
| 120 | + pub left: Option<Rc<RefCell<TreeNode>>>, |
| 121 | + pub right: Option<Rc<RefCell<TreeNode>>>, |
| 122 | +} |
| 123 | + |
| 124 | +impl TreeNode { |
| 125 | + #[inline] |
| 126 | + pub fn new(val: i32) -> Self { |
| 127 | + TreeNode { |
| 128 | + val, |
| 129 | + left: None, |
| 130 | + right: None, |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments