1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{cell::RefCell, rc::Rc};
/// ## Description
///
/// When writing the nodes you need to start from the lowest ones and make your way up to the root
/// one.
///
/// There's no guarantees that this Binary Tree is balanced (this is important for optimizations.)
///
/// The struct signature is entirely 1:1 with LeetCode with additional optional features like
/// serde.
///
/// ## Examples
///
/// Example 1 - balanced tree:
///
/// ```rust
/// use std::{cell::RefCell, rc::Rc};
/// use leetcode_trees_rs::utils::TreeNode;
///
/// let left_node = TreeNode::new(42);
/// let right_node = TreeNode::new(42);
/// let root_node = TreeNode {
/// val: 100,
/// left: Some(Rc::from(RefCell::from(left_node))),
/// right: Some(Rc::from(RefCell::from(right_node))),
/// };
/// ```
///
/// Result:
///
/// ```markdown
/// Some(100) // Root Node
/// / \
/// Some(42) Some(42) // Left and Right nodes respectively
/// / \ / \
/// None None None None
/// ```
///
/// **NOTE:** Simplified `Option<Rc<RefCell<TreeNode>>>` to `Option<TreeNode.val>`.
///
/// Example 2 - unbalanced tree:
///
/// ```rust
/// use std::{cell::RefCell, rc::Rc};
/// use leetcode_trees_rs::utils::TreeNode;
///
/// let left_node = TreeNode::new(42);
/// let right_node = TreeNode::new(21);
/// let root_node = TreeNode {
/// val: 100,
/// left: Some(Rc::from(RefCell::from(left_node))),
/// right: Some(Rc::from(RefCell::from(right_node))),
/// };
/// ```
///
/// Result:
///
/// ```markdown
/// Some(100) // Root Node
/// / \
/// Some(42) Some(21) // Left and Right nodes respectively
/// / \ / \
/// None None None None
/// ```
///
/// Example 3 - unbalanced tree:
///
/// ```rust
/// use std::{cell::RefCell, rc::Rc};
/// use leetcode_trees_rs::utils::TreeNode;
///
/// // let left_node = TreeNode::new(42); // Removing the left node.
/// let right_node = TreeNode::new(21);
/// let root_node = TreeNode {
/// val: 100,
/// left: None, // Setting the left node to None.
/// right: Some(Rc::from(RefCell::from(right_node))),
/// };
/// ```
///
/// Result:
///
/// ```markdown
/// Some(100) // Root Node
/// / \
/// None Some(21) // Left and Right nodes respectively
/// / \
/// None None
/// ```
///
/// The second example is unbalanced because the depths of both sides don't match.
///
/// Example 4 - unbalanced tree:
///
/// ```rust
/// use std::{cell::RefCell, rc::Rc};
/// use leetcode_trees_rs::utils::TreeNode;
///
/// let left_right_node = TreeNode::new(16);
/// let left_node = TreeNode {
/// val: 42,
/// left: None,
/// right: Some(Rc::from(RefCell::from(left_right_node))),
/// };
/// let right_node = TreeNode::new(21);
/// let root_node = TreeNode {
/// val: 100,
/// left: Some(Rc::from(RefCell::from(left_node))),
/// right: Some(Rc::from(RefCell::from(right_node))),
/// };
/// ```
///
/// Result:
///
/// ```markdown
/// Some(100) // Root Node
/// / \
/// Some(42) Some(21) // Left and Right nodes respectively
/// / \ / \
/// None Some(16) None None // Left->Left; Left->Right;
/// / \ // Right->Left; Right->right
/// None None
/// ```
///
/// Example 5 - balanced tree:
///
/// ```rust
/// use std::{cell::RefCell, rc::Rc};
/// use leetcode_trees_rs::utils::TreeNode;
///
/// let left_left_node = TreeNode::new(16);
/// let right_right_node = TreeNode::new(16);
/// let left_node = TreeNode {
/// val: 42,
/// left: Some(Rc::from(RefCell::from(left_left_node))),
/// right: None,
/// };
/// let right_node = TreeNode {
/// val: 42,
/// left: None,
/// right: Some(Rc::from(RefCell::from(right_right_node))),
/// };
/// let root_node = TreeNode {
/// val: 100,
/// left: Some(Rc::from(RefCell::from(left_node))),
/// right: Some(Rc::from(RefCell::from(right_node))),
/// };
/// ```
///
/// Result:
///
/// ```markdown
/// Some(100) // Root Node
/// / \
/// Some(42) Some(42) // Left and Right nodes respectively
/// / \ / \
/// Some(16) None None Some(16) // Left->Left; Left->Right;
/// / \ / \ // Right->Left; Right->right
/// None None None None
/// ```
///
/// If root->right.val != root->left.val then the tree wouldn't be balanced.
///
/// The same is the case if root->left->left.val != root->right->right.val.
///
/// ## Big O
///
/// Traversal (postorder, preorder and inorder) -> O(n)
///
/// Insertion at the start (above the old root) -> O(1)
///
/// Any other insertion:
/// - balanced -> O(log n)
/// - unbalanced -> O(n)
///
/// Searching:
/// - balanced -> O(log n)
/// - unbalanced -> O(n)
///
/// Deletion:
/// - balanced -> O(log n)
/// - unbalanced -> O(n)
///
/// Finding successor/predecessor:
/// - balanced -> O(log n)
/// - unbalanced -> O(n)
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TreeNode {
/// This uses normal i32 values that can be serialized and deserialized using serde if wanted.
pub val: i32,
/// An optional smart pointer contained within a reference cell. This provides very useful
/// functionality like interior mutability. The poiter can be represented as the left child
/// node of a binary tree.
#[cfg_attr(feature = "serde", serde(skip))]
pub left: Option<Rc<RefCell<TreeNode>>>,
/// An optional smart pointer contained within a reference cell. This provides very useful
/// functionality like interior mutability. The poiter can be represented as the right child
/// node of a binary tree.
#[cfg_attr(feature = "serde", serde(skip))]
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
/// Used to make a new TreeNode with next as `None`.
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}