1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
//! Binary Tree Preorder Traversal[leetcode: binary_tree_preorder_traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) //! //! Given a binary tree, return the *preorder* traversal of its nodes' values. //! //! **Example:** //! //! ``` //! Input: [1,null,2,3] //! 1 //! \ //! 2 //! / //! 3 //! //! Output: [1,2,3] //! ``` //! **Follow up:** Recursive solution is trivial, could you do it iteratively? /// # Solutions /// /// # Approach 1: Recursive Approach /// /// * Time complexity: O(n) /// /// * Space complexity: O(logn) /// /// * Runtime: 0 ms /// /// * Memory: 2.4 MB /// /// ```rust /// // Definition for a binary tree node. /// // #[derive(Debug, PartialEq, Eq)] /// // pub struct TreeNode { /// // pub val: i32, /// // pub left: Option<Rc<RefCell<TreeNode>>>, /// // pub right: Option<Rc<RefCell<TreeNode>>>, /// // } /// // /// // impl TreeNode { /// // #[inline] /// // pub fn new(val: i32) -> Self { /// // TreeNode { /// // val, /// // left: None, /// // right: None /// // } /// // } /// // } /// use std::rc::Rc; /// use std::cell::RefCell; /// impl Solution { /// pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> { /// let mut result: Vec<i32> = vec![]; /// if root.is_none() { return result; } /// /// Self::_post_order(root, &mut result); /// result /// } /// fn _post_order(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) { /// match root { /// Some(node) => { /// result.push(node.borrow().val); /// Self::_post_order(node.borrow().left.clone(), result); /// Self::_post_order(node.borrow().right.clone(), result); /// }, /// None => { return; } /// } /// } /// } /// ``` /// /// # Approach 2: Iterating method using Stack /// /// * Time complexity: O(n) /// /// * Space complexity: O(n) /// /// * Runtime: 0 ms /// /// * Memory: 2.4 MB /// /// ```rust /// // Definition for a binary tree node. /// // #[derive(Debug, PartialEq, Eq)] /// // pub struct TreeNode { /// // pub val: i32, /// // pub left: Option<Rc<RefCell<TreeNode>>>, /// // pub right: Option<Rc<RefCell<TreeNode>>>, /// // } /// // /// // impl TreeNode { /// // #[inline] /// // pub fn new(val: i32) -> Self { /// // TreeNode { /// // val, /// // left: None, /// // right: None /// // } /// // } /// // } /// use std::rc::Rc; /// use std::cell::RefCell; /// impl Solution { /// pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> { /// let mut result = vec![]; /// if root.is_none() { return result; } /// /// let mut stack: Vec<Rc<RefCell<TreeNode>>> = Vec::new(); /// let mut r = root.clone(); /// while r.is_some() || !stack.is_empty() { /// while let Some(node) = r { /// result.push(node.borrow().val); /// stack.push(node.clone()); /// r = node.borrow().left.clone(); /// } /// r = stack.pop(); /// if let Some(node) = r { /// r = node.borrow().right.clone(); /// } /// } /// result /// /// } /// } /// ``` /// pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> { let mut result = vec![]; if root.is_none() { return result; } let mut stack: Vec<Rc<RefCell<TreeNode>>> = Vec::new(); let mut r = root.clone(); while r.is_some() || !stack.is_empty() { while let Some(node) = r { result.push(node.borrow().val); stack.push(node.clone()); r = node.borrow().left.clone(); } r = stack.pop(); if let Some(node) = r { r = node.borrow().right.clone(); } } result } use std::rc::Rc; use std::cell::RefCell; // Definition for a binary tree node. #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None } } }