Struct leetcode_test_utils::tree::diagnosis::T [−][src]
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.
Implementations
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);
Returns the post-order of the binary tree.
Returns the level order of the binary tree.
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);
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);
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())
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());
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]");
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)]);
Trait Implementations
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more