#94
Easy Algorithms Binary tree inorder traversal
Stack Tree Depth-First Search Binary Tree
79.8% acceptance
Feb 27, 2026
14828
895
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Solution
Rust
Time O(n)
Space 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::inorder(root.as_ref(), &mut result);
result
}
fn inorder(node: Option<&Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
if let Some(n) = node {
let n = n.borrow();
Self::inorder(n.left.as_ref(), result);
result.push(n.val);
Self::inorder(n.right.as_ref(), result);
}
}
}