use std::rc::Rc; use std::cell::RefCell; #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option>>, pub right: Option>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None } } } struct Solution; impl Solution { pub fn preorder_traversal(root: Option>>) -> Vec { let mut ans: Vec = Vec::new(); let mut stack: Vec>>> = vec![root]; while let Some(last) = stack.pop() { if let Some(n) = last { ans.push(n.borrow().val); stack.push(n.borrow().right.clone()); stack.push(n.borrow().left.clone()); } } ans } }