Skip to content

Commit 9288a63

Browse files
committed
LeetCode 226. Invert Binary Tree Rust
1 parent 6bb896a commit 9288a63

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
151151
| [219. Contains Duplicate II][lc219] | 🟢 Easy | [![python](res/py.png)][lc219py] |
152152
| [222. Count Complete Tree Nodes][lc222] | 🟠 Medium | [![python](res/py.png)][lc222py] |
153153
| [223. Rectangle Area][lc223] | 🟠 Medium | [![python](res/py.png)][lc223py] |
154-
| [226. Invert Binary Tree][lc226] | 🟢 Easy | [![python](res/py.png)][lc226py] |
154+
| [226. Invert Binary Tree][lc226] | 🟢 Easy | [![python](res/py.png)][lc226py] [![rust](res/rs.png)][lc226rs] |
155155
| [227. Basic Calculator II][lc227] | 🟠 Medium | [![python](res/py.png)][lc227py] |
156156
| [230. Kth Smallest Element in a BST][lc230] | 🟠 Medium | [![python](res/py.png)][lc230py] |
157157
| [232. Implement Queue using Stacks][lc232] | 🟢 Easy | [![python](res/py.png)][lc232py] |
@@ -700,6 +700,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
700700
[lc223py]: leetcode/rectangle-area.py
701701
[lc226]: https://leetcode.com/problems/invert-binary-tree/
702702
[lc226py]: leetcode/invert-binary-tree.py
703+
[lc226rs]: leetcode/invert-binary-tree.rs
703704
[lc227]: https://leetcode.com/problems/basic-calculator-ii/
704705
[lc227py]: leetcode/basic-calculator-ii.py
705706
[lc230]: https://leetcode.com/problems/kth-smallest-element-in-a-bst/

leetcode/invert-binary-tree.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
3333
return root
3434

3535

36+
# Use depth-first search to visit all nodes in the tree, for each node,
37+
# swap the position of its children, recursively call the function on
38+
# the children.
39+
#
40+
# Time complexity: O(n) - We visit each node in the tree and, for each,
41+
# do O(1) work.
42+
# Space complexity: O(h) - The call stack can grow to the height of the
43+
# tree, which could be the same as n.
44+
#
45+
# Runtime 38 ms Beats 33.89%
46+
# Memory 13.8 MB Beats 94.48%
47+
class RecursiveInPlace:
48+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
49+
if not root:
50+
return None
51+
root.left, root.right = self.invertTree(root.right), self.invertTree(
52+
root.left
53+
)
54+
return root
55+
56+
3657
# Use depth-first search to visit all nodes in the tree, for each node,
3758
# swap the position of its children.
3859
#
@@ -42,7 +63,7 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
4263
# which could be the same as n.
4364
#
4465
# Runtime 31 ms Beats 92.4%
45-
# Memory 13.9 MB Beats 57.73%
66+
# Memory 13.8 MB Beats 94.98%
4667
class IterativeDFS:
4768
def invertTree(self, root):
4869
stack = [root]
@@ -59,6 +80,7 @@ def test():
5980
executors = [
6081
IterativeDFS,
6182
RecursiveDFS,
83+
RecursiveInPlace,
6284
]
6385
tests = [
6486
[[], []],

leetcode/invert-binary-tree.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 226. Invert Binary Tree
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/invert-binary-tree/
5+
//
6+
// Tags: Tree - Depth-First Search - Breadth-First Search - Binary Tree
7+
8+
use std::cell::RefCell;
9+
use std::rc::Rc;
10+
11+
struct Solution;
12+
impl Solution {
13+
// Use depth-first search to visit all nodes in the tree, for each node,
14+
// swap the position of its children.
15+
//
16+
// Time complexity: O(n) - We visit each node in the tree and, for each,
17+
// do O(1) work.
18+
// Space complexity: O(h) - The call stack can grow to the height of the
19+
// tree, which could be the same as n.
20+
//
21+
// Runtime 1 ms Beats 66.37%
22+
// Memory 2 MB Beats 65.49%
23+
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
24+
match root {
25+
Some(node_ref) => {
26+
let tmp = node_ref.borrow();
27+
Some(Rc::new(RefCell::new(TreeNode {
28+
val: tmp.val,
29+
left: Self::invert_tree(tmp.right.clone()),
30+
right: Self::invert_tree(tmp.left.clone()),
31+
})))
32+
}
33+
None => None,
34+
}
35+
}
36+
}
37+
38+
// Tests.
39+
fn main() {
40+
// assert_eq!(Solution::min_diff_in_bst(root), 1);
41+
println!("All tests passed!")
42+
}
43+
44+
// Definition for a binary tree node.
45+
#[derive(Debug, PartialEq, Eq)]
46+
pub struct TreeNode {
47+
pub val: i32,
48+
pub left: Option<Rc<RefCell<TreeNode>>>,
49+
pub right: Option<Rc<RefCell<TreeNode>>>,
50+
}
51+
52+
impl TreeNode {
53+
#[inline]
54+
pub fn new(val: i32) -> Self {
55+
TreeNode {
56+
val,
57+
left: None,
58+
right: None,
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)