144. Binary Tree Preorder Traversal Easy

@problem@discussion
#Stack#Tree#Depth-First Search#Binary Tree



1/**
2 * [144] Binary Tree Preorder Traversal
3 *
4 * Given the root of a binary tree, return the preorder traversal of its nodes' values.
5 *  
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 125px; height: 200px;" />
8 * Input: root = [1,null,2,3]
9 * Output: [1,2,3]
10 * 
11 * Example 2:
12 * 
13 * Input: root = []
14 * Output: []
15 * 
16 * Example 3:
17 * 
18 * Input: root = [1]
19 * Output: [1]
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	The number of nodes in the tree is in the range [0, 100].
25 * 	-100 <= Node.val <= 100
26 * 
27 *  
28 * Follow up: Recursive solution is trivial, could you do it iteratively?
29 * 
30 */
31pub struct Solution {}
32use crate::util::tree::{TreeNode, to_tree};
33
34// problem: https://leetcode.com/problems/binary-tree-preorder-traversal/
35// discuss: https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39// Definition for a binary tree node.
40// #[derive(Debug, PartialEq, Eq)]
41// pub struct TreeNode {
42//   pub val: i32,
43//   pub left: Option<Rc<RefCell<TreeNode>>>,
44//   pub right: Option<Rc<RefCell<TreeNode>>>,
45// }
46// 
47// impl TreeNode {
48//   #[inline]
49//   pub fn new(val: i32) -> Self {
50//     TreeNode {
51//       val,
52//       left: None,
53//       right: None
54//     }
55//   }
56// }
57use std::rc::Rc;
58use std::cell::RefCell;
59impl Solution {
60    pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
61        vec![]
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_144() {
73    }
74}
75


Back
© 2025 bowen.ge All Rights Reserved.