Macro leetcode_trees_rs::utils::symmetric_tree
source · macro_rules! symmetric_tree { ($val:expr) => { ... }; ($val:expr, $($both_sides:tt)*) => { ... }; }
Expand description
A re-export for the symmetric_tree!, right_tree! and left_tree! macros.
All of the TreeNode macros can be used to also just generate a new TreeNode
instance without
expanding on it.
§Description
A macro to reduce the boilerplate in generating symmetric binary trees.
§Match arms
Arm 1:
- Takes the value as an argument.
- Equivalent of doing
TreeNode::new()
. Arm 2: - Takes the value as an argument.
- Also takes a sequence of left and right node values at the same time (which means they’re
symmetric) as an argument (and builds the
TreeNode
struct with them).
§Example usage
use leetcode_trees_rs::utils::symmetric_tree;
symmetric_tree!(1, 2, 3, 4);
The symmetric_tree! macro invocation is desugared to this:
use std::{rc::Rc, cell::RefCell, boxed::Box};
use leetcode_trees_rs::utils::{symmetric_tree, TreeNode};
let node = TreeNode {
val: 1,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 2,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 3,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 3,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
}))),
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 2,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 3,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 3,
left: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
right: Some(Rc::new(RefCell::new(TreeNode{
val: 4,
left: None,
right: None,
}))),
}))),
}))),
};
assert_eq!(node, symmetric_tree!(1, 2, 3, 4));
Now you have a tree that branches all the way through the right side without having anything on the left.