pub struct T(pub TreeHandle);
Expand description
Zero cost wrapper for Option<Rc<RefCell<TreeNode>>>
, also for bypassing the orphan rule.
There are many useful methods for operating on the binary tree as well.
Tuple Fields§
§0: TreeHandle
Implementations§
Source§impl T
impl T
Sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
Get the height for the binary tree.
Returns the height of the binary tree. The empty tree has height 0.
§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
let tree1 = btree!(1, 2, 3);
assert_eq!(T(tree1).height(), 2);
assert_eq!(T(None).height(), 0);
Sourcepub fn post_order(&self) -> Vec<i32>
pub fn post_order(&self) -> Vec<i32>
Returns the post-order of the binary tree.
Sourcepub fn level_order(&self) -> Vec<i32>
pub fn level_order(&self) -> Vec<i32>
Returns the level order of the binary tree.
Sourcepub fn re_owned(&mut self)
pub fn re_owned(&mut self)
Launder the binary tree.
Replace the current binary tree with a new representation, in which the structure and values is
preserved respectively, but every reachable Rc
will only have 1 strong count.
This is helpful if you do not want the value in your tree changed through
Rc<RefCell<TreeNode>>
elsewhere.
§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
use std::rc::Rc;
let tree = T(btree!(3));
let evil = Rc::clone(tree.0.as_ref().unwrap());
// the action below changes the value handled in `tree`, which may be unexpected
evil.borrow_mut().val = 42;
assert_ne!(tree.0.unwrap().borrow().val, 3);
Sourcepub fn detach(&self) -> Self
pub fn detach(&self) -> Self
Get the mirror tree.
Returns a binary tree sharing the same structure and values handled by self
except that
every reachable Rc
will only have 1 strong count.
This is helpful if you want to get the tree structure without worrying about the values be soon changed by code elsewhere.
§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
use std::rc::Rc;
let tree = T(btree!(3));
let cannot_invade = tree.detach();
cannot_invade.0.as_ref().unwrap().borrow_mut().val = 42;
assert_eq!(tree.0.unwrap().borrow().val, 3);
Sourcepub fn is_balanced(&self) -> bool
pub fn is_balanced(&self) -> bool
Test if the binary tree is balanced.
§Example
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
let tree1 = T(btree!(4, 2));
assert!(tree1.is_balanced());
let tree2 = T(btree!(4, 2, null, 1));
assert!(!tree2.is_balanced())
Sourcepub fn is_binary_search_tree(&self) -> bool
pub fn is_binary_search_tree(&self) -> bool
Test if the binary tree is a BST(binary search tree).
§Examples
use leetcode_test_utils::btree;
use leetcode_test_utils::tree::T;
let tree = T(btree!(5, 2, 9, 1));
assert!(tree.is_binary_search_tree());
Sourcepub fn to_leetcode_raw(&self) -> String
pub fn to_leetcode_raw(&self) -> String
Returns the leetcode representation of the handled binary tree.
§Examples
use leetcode_test_utils::tree::T;
use leetcode_test_utils::btree;
let tree = T(btree!(2, 4, null, 9));
assert_eq!(tree.to_leetcode_raw(), "[2,4,null,9]");
Sourcepub fn to_leetcode(&self) -> Vec<Option<i32>>
pub fn to_leetcode(&self) -> Vec<Option<i32>>
Returns the parsed leetcode representation of the handled binary tree.
§Examples
use leetcode_test_utils::tree::T;
use leetcode_test_utils::btree;
let tree = T(btree!(2, 4, null, 9));
assert_eq!(tree.to_leetcode(), vec![Some(2), Some(4), None, Some(9)]);