From 40436dc53e50c7b0eec0a7a925ba697d69ec3609 Mon Sep 17 00:00:00 2001 From: Dale Seo Date: Sun, 18 Jan 2026 07:44:32 -0500 Subject: [PATCH] invert-binary-tree --- invert-binary-tree/DaleSeo.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 invert-binary-tree/DaleSeo.rs diff --git a/invert-binary-tree/DaleSeo.rs b/invert-binary-tree/DaleSeo.rs new file mode 100644 index 000000000..a88d1d205 --- /dev/null +++ b/invert-binary-tree/DaleSeo.rs @@ -0,0 +1,25 @@ +// Definition for a binary tree node. +#[derive(Debug, PartialEq, Eq)] +pub struct TreeNode { + pub val: i32, + pub left: Option>>, + pub right: Option>>, +} + +use std::rc::Rc; +use std::cell::RefCell; + +// TC: O(n) +// SC: O(n) +impl Solution { + pub fn invert_tree(root: Option>>) -> Option>> { + if let Some(node) = root.as_ref() { + let left = node.borrow().left.clone(); + let right = node.borrow().right.clone(); + + node.borrow_mut().left = Solution::invert_tree(right); + node.borrow_mut().right = Solution::invert_tree(left); + } + root + } +}