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 pre-order of the binary tree.

Returns the in-order of the binary tree.

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)]);

Returns the len of the 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.len(), 3);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.