From 9f4f02ac2489c487227b0cf2d3e9edf28c9b8b63 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 8 Sep 2022 13:59:46 +0200 Subject: [PATCH] =?UTF-8?q?problems(rs):=20add=20=E2=80=9E94.=20Binary=20T?= =?UTF-8?q?ree=20Inorder=20Traversal=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/binary-tree-inorder-traversal.rs | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 problems/binary-tree-inorder-traversal.rs diff --git a/problems/binary-tree-inorder-traversal.rs b/problems/binary-tree-inorder-traversal.rs new file mode 100644 index 0000000..96d685a --- /dev/null +++ b/problems/binary-tree-inorder-traversal.rs @@ -0,0 +1,40 @@ +// Definition for a binary tree node. +// #[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 +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + fn inorder(values: &mut Vec, node: Option<&Rc>>) { + match node { + None => {}, + Some(n) => { + Solution::inorder(values, (*n).borrow().left.as_ref()); + values.push((*n).borrow().val); + Solution::inorder(values, (*n).borrow().right.as_ref()); + } + } + } + + pub fn inorder_traversal(root: Option>>) -> Vec { + let mut values: Vec = Vec::new(); + + Solution::inorder(&mut values, root.as_ref()); + + values + } +}