-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree_inorder_traversal.rs
69 lines (68 loc) · 1.58 KB
/
binary_tree_inorder_traversal.rs
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
///
/// Problem: Binary Tree Inorder Traversal
///
/// Given the root of a binary tree, return the inorder traversal of its nodes' values.
///
/// **Example 1:**
/// ```plaintext
/// Input: root = [1,null,2,3]
/// Output: [1,3,2]
/// Explanation: Inorder traversal visits nodes in the order: left → root → right.
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: root = []
/// Output: []
/// ```
///
/// **Example 3:**
/// ```plaintext
/// Input: root = [1]
/// Output: [1]
/// ```
///
/// **Constraints:**
/// - The number of nodes in the tree is in the range `[0, 100]`.
/// - `-100 <= Node.val <= 100`
///
/// # Solution:
/// - **Time Complexity:** `O(n)`
/// - **Space Complexity:** `O(n)`
///
///
// 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 inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = Vec::new();
Self::dfs(&root, &mut result);
result
}
fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
if let Some(n) = node {
let n = n.borrow();
Self::dfs(&n.left, result);
result.push(n.val);
Self::dfs(&n.right, result);
}
}
}