diff --git a/solution/0100-0199/0101.Symmetric Tree/README.md b/solution/0100-0199/0101.Symmetric Tree/README.md index 800a5c5ba45ee..b5b7a2e6c74c7 100644 --- a/solution/0100-0199/0101.Symmetric Tree/README.md +++ b/solution/0100-0199/0101.Symmetric Tree/README.md @@ -58,13 +58,13 @@ tags: ### 方法一:递归 -我们设计一个函数 $dfs(root1, root2)$,用于判断两个二叉树是否对称。答案即为 $dfs(root, root)$。 +我们设计一个函数 $\textit{dfs}(\textit{root1}, \textit{root2})$,用于判断两个二叉树是否对称。答案即为 $\textit{dfs}(\textit{root.left}, \textit{root.right})$。 -函数 $dfs(root1, root2)$ 的逻辑如下: +函数 $\textit{dfs}(\textit{root1}, \textit{root2})$ 的逻辑如下: -- 如果 $root1$ 和 $root2$ 都为空,则两个二叉树对称,返回 `true`; -- 如果 $root1$ 和 $root2$ 中只有一个为空,或者 $root1.val \neq root2.val$,则两个二叉树不对称,返回 `false`; -- 否则,判断 $root1$ 的左子树和 $root2$ 的右子树是否对称,以及 $root1$ 的右子树和 $root2$ 的左子树是否对称,这里使用了递归。 +- 如果 $\textit{root1}$ 和 $\textit{root2}$ 都为空,则两个二叉树对称,返回 `true`; +- 如果 $\textit{root1}$ 和 $\textit{root2}$ 中只有一个为空,或者 $\textit{root1.val} \neq \textit{root2.val}$ +- 否则,判断 $\textit{root1}$ 的左子树和 $\textit{root2}$ 的右子树是否对称,以及 $\textit{root1}$ 的右子树和 $\textit{root2}$ 的左子树是否对称,这里使用了递归。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。 @@ -81,14 +81,14 @@ tags: # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: - def dfs(root1, root2): - if root1 is None and root2 is None: + def dfs(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: + if root1 == root2: return True if root1 is None or root2 is None or root1.val != root2.val: return False return dfs(root1.left, root2.right) and dfs(root1.right, root2.left) - return dfs(root, root) + return dfs(root.left, root.right) ``` #### Java @@ -111,11 +111,11 @@ class Solution: */ class Solution { public boolean isSymmetric(TreeNode root) { - return dfs(root, root); + return dfs(root.left, root.right); } private boolean dfs(TreeNode root1, TreeNode root2) { - if (root1 == null && root2 == null) { + if (root1 == root2) { return true; } if (root1 == null || root2 == null || root1.val != root2.val) { @@ -143,12 +143,16 @@ class Solution { class Solution { public: bool isSymmetric(TreeNode* root) { - function dfs = [&](TreeNode* root1, TreeNode* root2) -> bool { - if (!root1 && !root2) return true; - if (!root1 || !root2 || root1->val != root2->val) return false; + auto dfs = [&](this auto&& dfs, TreeNode* root1, TreeNode* root2) -> bool { + if (root1 == root2) { + return true; + } + if (!root1 || !root2 || root1->val != root2->val) { + return false; + } return dfs(root1->left, root2->right) && dfs(root1->right, root2->left); }; - return dfs(root, root); + return dfs(root->left, root->right); } }; ``` @@ -165,9 +169,9 @@ public: * } */ func isSymmetric(root *TreeNode) bool { - var dfs func(*TreeNode, *TreeNode) bool + var dfs func(root1, root2 *TreeNode) bool dfs = func(root1, root2 *TreeNode) bool { - if root1 == nil && root2 == nil { + if root1 == root2 { return true } if root1 == nil || root2 == nil || root1.Val != root2.Val { @@ -175,7 +179,7 @@ func isSymmetric(root *TreeNode) bool { } return dfs(root1.Left, root2.Right) && dfs(root1.Right, root2.Left) } - return dfs(root, root) + return dfs(root.Left, root.Right) } ``` @@ -196,17 +200,16 @@ func isSymmetric(root *TreeNode) bool { * } */ -const dfs = (root1: TreeNode | null, root2: TreeNode | null) => { - if (root1 == root2) { - return true; - } - if (root1 == null || root2 == null || root1.val != root2.val) { - return false; - } - return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); -}; - function isSymmetric(root: TreeNode | null): boolean { + const dfs = (root1: TreeNode | null, root2: TreeNode | null): boolean => { + if (root1 === root2) { + return true; + } + if (!root1 || !root2 || root1.val !== root2.val) { + return false; + } + return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); + }; return dfs(root.left, root.right); } ``` @@ -235,23 +238,25 @@ function isSymmetric(root: TreeNode | null): boolean { use std::cell::RefCell; use std::rc::Rc; impl Solution { - fn dfs(root1: &Option>>, root2: &Option>>) -> bool { - if root1.is_none() && root2.is_none() { - return true; - } - if root1.is_none() || root2.is_none() { - return false; + pub fn is_symmetric(root: Option>>) -> bool { + fn dfs(root1: Option>>, root2: Option>>) -> bool { + match (root1, root2) { + (Some(node1), Some(node2)) => { + let node1 = node1.borrow(); + let node2 = node2.borrow(); + node1.val == node2.val + && dfs(node1.left.clone(), node2.right.clone()) + && dfs(node1.right.clone(), node2.left.clone()) + } + (None, None) => true, + _ => false, + } } - let node1 = root1.as_ref().unwrap().borrow(); - let node2 = root2.as_ref().unwrap().borrow(); - node1.val == node2.val - && Self::dfs(&node1.left, &node2.right) - && Self::dfs(&node1.right, &node2.left) - } - pub fn is_symmetric(root: Option>>) -> bool { - let node = root.as_ref().unwrap().borrow(); - Self::dfs(&node.left, &node.right) + match root { + Some(root) => dfs(root.borrow().left.clone(), root.borrow().right.clone()), + None => true, + } } } ``` @@ -272,12 +277,16 @@ impl Solution { * @return {boolean} */ var isSymmetric = function (root) { - function dfs(root1, root2) { - if (!root1 && !root2) return true; - if (!root1 || !root2 || root1.val != root2.val) return false; + const dfs = (root1, root2) => { + if (root1 === root2) { + return true; + } + if (!root1 || !root2 || root1.val !== root2.val) { + return false; + } return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); - } - return dfs(root, root); + }; + return dfs(root.left, root.right); }; ``` @@ -285,66 +294,4 @@ var isSymmetric = function (root) { - - -### 方法二 - - - -#### Rust - -```rust -// 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::cell::RefCell; -use std::collections::VecDeque; -use std::rc::Rc; -impl Solution { - pub fn is_symmetric(root: Option>>) -> bool { - let root = root.unwrap(); - let mut node = root.as_ref().borrow_mut(); - let mut queue = VecDeque::new(); - queue.push_back([node.left.take(), node.right.take()]); - while let Some([root1, root2]) = queue.pop_front() { - if root1.is_none() && root2.is_none() { - continue; - } - if root1.is_none() || root2.is_none() { - return false; - } - if let (Some(node1), Some(node2)) = (root1, root2) { - let mut node1 = node1.as_ref().borrow_mut(); - let mut node2 = node2.as_ref().borrow_mut(); - if node1.val != node2.val { - return false; - } - queue.push_back([node1.left.take(), node2.right.take()]); - queue.push_back([node1.right.take(), node2.left.take()]); - } - } - true - } -} -``` - - - - - diff --git a/solution/0100-0199/0101.Symmetric Tree/README_EN.md b/solution/0100-0199/0101.Symmetric Tree/README_EN.md index 9fc42e2e1fd12..cdb332303dcc5 100644 --- a/solution/0100-0199/0101.Symmetric Tree/README_EN.md +++ b/solution/0100-0199/0101.Symmetric Tree/README_EN.md @@ -55,13 +55,13 @@ tags: ### Solution 1: Recursion -We design a function $dfs(root1, root2)$ to determine whether two binary trees are symmetric. The answer is $dfs(root, root)$. +We design a function $\textit{dfs}(\textit{root1}, \textit{root2})$ to determine whether two binary trees are symmetric. The answer is $\textit{dfs}(\textit{root.left}, \textit{root.right})$. -The logic of the function $dfs(root1, root2)$ is as follows: +The logic of the function $\textit{dfs}(\textit{root1}, \textit{root2})$ is as follows: -- If both $root1$ and $root2$ are null, then the two binary trees are symmetric, return `true`. -- If only one of $root1$ and $root2$ is null, or if $root1.val \neq root2.val$, then the two binary trees are not symmetric, return `false`. -- Otherwise, determine whether the left subtree of $root1$ is symmetric to the right subtree of $root2$, and whether the right subtree of $root1$ is symmetric to the left subtree of $root2$. Here we use recursion. +- If both $\textit{root1}$ and $\textit{root2}$ are null, the two binary trees are symmetric, and we return `true`; +- If only one of $\textit{root1}$ and $\textit{root2}$ is null, or $\textit{root1.val} \neq \textit{root2.val}$, we return `false`; +- Otherwise, we check whether the left subtree of $\textit{root1}$ is symmetric with the right subtree of $\textit{root2}$, and whether the right subtree of $\textit{root1}$ is symmetric with the left subtree of $\textit{root2}$, using recursion. The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. @@ -78,14 +78,14 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: - def dfs(root1, root2): - if root1 is None and root2 is None: + def dfs(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: + if root1 == root2: return True if root1 is None or root2 is None or root1.val != root2.val: return False return dfs(root1.left, root2.right) and dfs(root1.right, root2.left) - return dfs(root, root) + return dfs(root.left, root.right) ``` #### Java @@ -108,11 +108,11 @@ class Solution: */ class Solution { public boolean isSymmetric(TreeNode root) { - return dfs(root, root); + return dfs(root.left, root.right); } private boolean dfs(TreeNode root1, TreeNode root2) { - if (root1 == null && root2 == null) { + if (root1 == root2) { return true; } if (root1 == null || root2 == null || root1.val != root2.val) { @@ -140,12 +140,16 @@ class Solution { class Solution { public: bool isSymmetric(TreeNode* root) { - function dfs = [&](TreeNode* root1, TreeNode* root2) -> bool { - if (!root1 && !root2) return true; - if (!root1 || !root2 || root1->val != root2->val) return false; + auto dfs = [&](this auto&& dfs, TreeNode* root1, TreeNode* root2) -> bool { + if (root1 == root2) { + return true; + } + if (!root1 || !root2 || root1->val != root2->val) { + return false; + } return dfs(root1->left, root2->right) && dfs(root1->right, root2->left); }; - return dfs(root, root); + return dfs(root->left, root->right); } }; ``` @@ -162,9 +166,9 @@ public: * } */ func isSymmetric(root *TreeNode) bool { - var dfs func(*TreeNode, *TreeNode) bool + var dfs func(root1, root2 *TreeNode) bool dfs = func(root1, root2 *TreeNode) bool { - if root1 == nil && root2 == nil { + if root1 == root2 { return true } if root1 == nil || root2 == nil || root1.Val != root2.Val { @@ -172,7 +176,7 @@ func isSymmetric(root *TreeNode) bool { } return dfs(root1.Left, root2.Right) && dfs(root1.Right, root2.Left) } - return dfs(root, root) + return dfs(root.Left, root.Right) } ``` @@ -193,17 +197,16 @@ func isSymmetric(root *TreeNode) bool { * } */ -const dfs = (root1: TreeNode | null, root2: TreeNode | null) => { - if (root1 == root2) { - return true; - } - if (root1 == null || root2 == null || root1.val != root2.val) { - return false; - } - return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); -}; - function isSymmetric(root: TreeNode | null): boolean { + const dfs = (root1: TreeNode | null, root2: TreeNode | null): boolean => { + if (root1 === root2) { + return true; + } + if (!root1 || !root2 || root1.val !== root2.val) { + return false; + } + return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); + }; return dfs(root.left, root.right); } ``` @@ -232,23 +235,25 @@ function isSymmetric(root: TreeNode | null): boolean { use std::cell::RefCell; use std::rc::Rc; impl Solution { - fn dfs(root1: &Option>>, root2: &Option>>) -> bool { - if root1.is_none() && root2.is_none() { - return true; - } - if root1.is_none() || root2.is_none() { - return false; + pub fn is_symmetric(root: Option>>) -> bool { + fn dfs(root1: Option>>, root2: Option>>) -> bool { + match (root1, root2) { + (Some(node1), Some(node2)) => { + let node1 = node1.borrow(); + let node2 = node2.borrow(); + node1.val == node2.val + && dfs(node1.left.clone(), node2.right.clone()) + && dfs(node1.right.clone(), node2.left.clone()) + } + (None, None) => true, + _ => false, + } } - let node1 = root1.as_ref().unwrap().borrow(); - let node2 = root2.as_ref().unwrap().borrow(); - node1.val == node2.val - && Self::dfs(&node1.left, &node2.right) - && Self::dfs(&node1.right, &node2.left) - } - pub fn is_symmetric(root: Option>>) -> bool { - let node = root.as_ref().unwrap().borrow(); - Self::dfs(&node.left, &node.right) + match root { + Some(root) => dfs(root.borrow().left.clone(), root.borrow().right.clone()), + None => true, + } } } ``` @@ -269,12 +274,16 @@ impl Solution { * @return {boolean} */ var isSymmetric = function (root) { - function dfs(root1, root2) { - if (!root1 && !root2) return true; - if (!root1 || !root2 || root1.val != root2.val) return false; + const dfs = (root1, root2) => { + if (root1 === root2) { + return true; + } + if (!root1 || !root2 || root1.val !== root2.val) { + return false; + } return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); - } - return dfs(root, root); + }; + return dfs(root.left, root.right); }; ``` @@ -282,66 +291,4 @@ var isSymmetric = function (root) { - - -### Solution 2 - - - -#### Rust - -```rust -// 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::cell::RefCell; -use std::collections::VecDeque; -use std::rc::Rc; -impl Solution { - pub fn is_symmetric(root: Option>>) -> bool { - let root = root.unwrap(); - let mut node = root.as_ref().borrow_mut(); - let mut queue = VecDeque::new(); - queue.push_back([node.left.take(), node.right.take()]); - while let Some([root1, root2]) = queue.pop_front() { - if root1.is_none() && root2.is_none() { - continue; - } - if root1.is_none() || root2.is_none() { - return false; - } - if let (Some(node1), Some(node2)) = (root1, root2) { - let mut node1 = node1.as_ref().borrow_mut(); - let mut node2 = node2.as_ref().borrow_mut(); - if node1.val != node2.val { - return false; - } - queue.push_back([node1.left.take(), node2.right.take()]); - queue.push_back([node1.right.take(), node2.left.take()]); - } - } - true - } -} -``` - - - - - diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.cpp b/solution/0100-0199/0101.Symmetric Tree/Solution.cpp index 6c3d01a561dff..86f3e8b209857 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.cpp +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.cpp @@ -12,11 +12,15 @@ class Solution { public: bool isSymmetric(TreeNode* root) { - function dfs = [&](TreeNode* root1, TreeNode* root2) -> bool { - if (!root1 && !root2) return true; - if (!root1 || !root2 || root1->val != root2->val) return false; + auto dfs = [&](this auto&& dfs, TreeNode* root1, TreeNode* root2) -> bool { + if (root1 == root2) { + return true; + } + if (!root1 || !root2 || root1->val != root2->val) { + return false; + } return dfs(root1->left, root2->right) && dfs(root1->right, root2->left); }; - return dfs(root, root); + return dfs(root->left, root->right); } -}; \ No newline at end of file +}; diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.go b/solution/0100-0199/0101.Symmetric Tree/Solution.go index b01f184063dfd..c7dba1b4b7bac 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.go +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.go @@ -7,9 +7,9 @@ * } */ func isSymmetric(root *TreeNode) bool { - var dfs func(*TreeNode, *TreeNode) bool + var dfs func(root1, root2 *TreeNode) bool dfs = func(root1, root2 *TreeNode) bool { - if root1 == nil && root2 == nil { + if root1 == root2 { return true } if root1 == nil || root2 == nil || root1.Val != root2.Val { @@ -17,5 +17,5 @@ func isSymmetric(root *TreeNode) bool { } return dfs(root1.Left, root2.Right) && dfs(root1.Right, root2.Left) } - return dfs(root, root) -} \ No newline at end of file + return dfs(root.Left, root.Right) +} diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.java b/solution/0100-0199/0101.Symmetric Tree/Solution.java index 87aecc84aca5c..e93ff69f5fbb5 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.java +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.java @@ -15,11 +15,11 @@ */ class Solution { public boolean isSymmetric(TreeNode root) { - return dfs(root, root); + return dfs(root.left, root.right); } private boolean dfs(TreeNode root1, TreeNode root2) { - if (root1 == null && root2 == null) { + if (root1 == root2) { return true; } if (root1 == null || root2 == null || root1.val != root2.val) { @@ -27,4 +27,4 @@ private boolean dfs(TreeNode root1, TreeNode root2) { } return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.js b/solution/0100-0199/0101.Symmetric Tree/Solution.js index 691b91438102a..47cf75c138c45 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.js +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.js @@ -11,10 +11,14 @@ * @return {boolean} */ var isSymmetric = function (root) { - function dfs(root1, root2) { - if (!root1 && !root2) return true; - if (!root1 || !root2 || root1.val != root2.val) return false; + const dfs = (root1, root2) => { + if (root1 === root2) { + return true; + } + if (!root1 || !root2 || root1.val !== root2.val) { + return false; + } return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); - } - return dfs(root, root); + }; + return dfs(root.left, root.right); }; diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.py b/solution/0100-0199/0101.Symmetric Tree/Solution.py index f6936aae7debc..75b2dde4e3b26 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.py +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.py @@ -6,11 +6,11 @@ # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: - def dfs(root1, root2): - if root1 is None and root2 is None: + def dfs(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: + if root1 == root2: return True if root1 is None or root2 is None or root1.val != root2.val: return False return dfs(root1.left, root2.right) and dfs(root1.right, root2.left) - return dfs(root, root) + return dfs(root.left, root.right) diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.rs b/solution/0100-0199/0101.Symmetric Tree/Solution.rs index 831f5da4e0d8c..1e1ace6179ca0 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.rs +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.rs @@ -19,22 +19,24 @@ use std::cell::RefCell; use std::rc::Rc; impl Solution { - fn dfs(root1: &Option>>, root2: &Option>>) -> bool { - if root1.is_none() && root2.is_none() { - return true; - } - if root1.is_none() || root2.is_none() { - return false; + pub fn is_symmetric(root: Option>>) -> bool { + fn dfs(root1: Option>>, root2: Option>>) -> bool { + match (root1, root2) { + (Some(node1), Some(node2)) => { + let node1 = node1.borrow(); + let node2 = node2.borrow(); + node1.val == node2.val + && dfs(node1.left.clone(), node2.right.clone()) + && dfs(node1.right.clone(), node2.left.clone()) + } + (None, None) => true, + _ => false, + } } - let node1 = root1.as_ref().unwrap().borrow(); - let node2 = root2.as_ref().unwrap().borrow(); - node1.val == node2.val - && Self::dfs(&node1.left, &node2.right) - && Self::dfs(&node1.right, &node2.left) - } - pub fn is_symmetric(root: Option>>) -> bool { - let node = root.as_ref().unwrap().borrow(); - Self::dfs(&node.left, &node.right) + match root { + Some(root) => dfs(root.borrow().left.clone(), root.borrow().right.clone()), + None => true, + } } } diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.ts b/solution/0100-0199/0101.Symmetric Tree/Solution.ts index d072acf9baa8f..127fc2ecb4dd5 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.ts +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.ts @@ -12,16 +12,15 @@ * } */ -const dfs = (root1: TreeNode | null, root2: TreeNode | null) => { - if (root1 == root2) { - return true; - } - if (root1 == null || root2 == null || root1.val != root2.val) { - return false; - } - return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); -}; - function isSymmetric(root: TreeNode | null): boolean { + const dfs = (root1: TreeNode | null, root2: TreeNode | null): boolean => { + if (root1 === root2) { + return true; + } + if (!root1 || !root2 || root1.val !== root2.val) { + return false; + } + return dfs(root1.left, root2.right) && dfs(root1.right, root2.left); + }; return dfs(root.left, root.right); } diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution2.rs b/solution/0100-0199/0101.Symmetric Tree/Solution2.rs deleted file mode 100644 index a1fb59abbc400..0000000000000 --- a/solution/0100-0199/0101.Symmetric Tree/Solution2.rs +++ /dev/null @@ -1,47 +0,0 @@ -// 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::cell::RefCell; -use std::collections::VecDeque; -use std::rc::Rc; -impl Solution { - pub fn is_symmetric(root: Option>>) -> bool { - let root = root.unwrap(); - let mut node = root.as_ref().borrow_mut(); - let mut queue = VecDeque::new(); - queue.push_back([node.left.take(), node.right.take()]); - while let Some([root1, root2]) = queue.pop_front() { - if root1.is_none() && root2.is_none() { - continue; - } - if root1.is_none() || root2.is_none() { - return false; - } - if let (Some(node1), Some(node2)) = (root1, root2) { - let mut node1 = node1.as_ref().borrow_mut(); - let mut node2 = node2.as_ref().borrow_mut(); - if node1.val != node2.val { - return false; - } - queue.push_back([node1.left.take(), node2.right.take()]); - queue.push_back([node1.right.take(), node2.left.take()]); - } - } - true - } -} diff --git a/solution/0200-0299/0226.Invert Binary Tree/README.md b/solution/0200-0299/0226.Invert Binary Tree/README.md index 3368249fdff33..4e8274262e172 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/README.md +++ b/solution/0200-0299/0226.Invert Binary Tree/README.md @@ -65,7 +65,7 @@ tags: ### 方法一:递归 -递归的思路很简单,就是交换当前节点的左右子树,然后递归地交换当前节点的左右子树。 +我们首先判断 $\textit{root}$ 是否为空,若为空则直接返回 $\text{null}$。然后递归地对树的左右子树进行翻转,将翻转后的右子树作为新的左子树,将翻转后的左子树作为新的右子树,返回 $\textit{root}$。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。 @@ -82,14 +82,10 @@ tags: # self.right = right class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - def dfs(root): - if root is None: - return - root.left, root.right = root.right, root.left - dfs(root.left) - dfs(root.right) - - dfs(root) + if root is None: + return None + l, r = self.invertTree(root.left), self.invertTree(root.right) + root.left, root.right = r, l return root ``` @@ -113,19 +109,14 @@ class Solution: */ class Solution { public TreeNode invertTree(TreeNode root) { - dfs(root); - return root; - } - - private void dfs(TreeNode root) { if (root == null) { - return; + return null; } - TreeNode t = root.left; - root.left = root.right; - root.right = t; - dfs(root.left); - dfs(root.right); + TreeNode l = invertTree(root.left); + TreeNode r = invertTree(root.right); + root.left = r; + root.right = l; + return root; } } ``` @@ -147,15 +138,13 @@ class Solution { class Solution { public: TreeNode* invertTree(TreeNode* root) { - function dfs = [&](TreeNode* root) { - if (!root) { - return; - } - swap(root->left, root->right); - dfs(root->left); - dfs(root->right); - }; - dfs(root); + if (!root) { + return root; + } + TreeNode* l = invertTree(root->left); + TreeNode* r = invertTree(root->right); + root->left = r; + root->right = l; return root; } }; @@ -173,16 +162,11 @@ public: * } */ func invertTree(root *TreeNode) *TreeNode { - var dfs func(*TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - root.Left, root.Right = root.Right, root.Left - dfs(root.Left) - dfs(root.Right) + if root == nil { + return root } - dfs(root) + l, r := invertTree(root.Left), invertTree(root.Right) + root.Left, root.Right = r, l return root } ``` @@ -205,15 +189,13 @@ func invertTree(root *TreeNode) *TreeNode { */ function invertTree(root: TreeNode | null): TreeNode | null { - const dfs = (root: TreeNode | null) => { - if (root === null) { - return; - } - [root.left, root.right] = [root.right, root.left]; - dfs(root.left); - dfs(root.right); - }; - dfs(root); + if (!root) { + return root; + } + const l = invertTree(root.left); + const r = invertTree(root.right); + root.left = r; + root.right = l; return root; } ``` @@ -239,23 +221,17 @@ function invertTree(root: TreeNode | null): TreeNode | null { // } // } // } -use std::cell::RefCell; use std::rc::Rc; +use std::cell::RefCell; impl Solution { - #[allow(dead_code)] pub fn invert_tree(root: Option>>) -> Option>> { - if root.is_none() { - return root; + if let Some(node) = root.clone() { + let mut node = node.borrow_mut(); + let left = node.left.take(); + let right = node.right.take(); + node.left = Self::invert_tree(right); + node.right = Self::invert_tree(left); } - let left = root.as_ref().unwrap().borrow().left.clone(); - let right = root.as_ref().unwrap().borrow().right.clone(); - // Invert the subtree - let inverted_left = Self::invert_tree(right); - let inverted_right = Self::invert_tree(left); - // Update the left & right - root.as_ref().unwrap().borrow_mut().left = inverted_left; - root.as_ref().unwrap().borrow_mut().right = inverted_right; - // Return the root root } } @@ -277,72 +253,40 @@ impl Solution { * @return {TreeNode} */ var invertTree = function (root) { - const dfs = root => { - if (!root) { - return; - } - [root.left, root.right] = [root.right, root.left]; - dfs(root.left); - dfs(root.right); - }; - dfs(root); + if (!root) { + return root; + } + const l = invertTree(root.left); + const r = invertTree(root.right); + root.left = r; + root.right = l; return root; }; ``` - - - +#### C# - - -### 方法二 - - - -#### Python3 - -```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -class Solution: - def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - if root is None: - return None - l, r = self.invertTree(root.left), self.invertTree(root.right) - root.left, root.right = r, l - return root -``` - -#### Java - -```java +```cs /** * Definition for a binary tree node. * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode() {} - * TreeNode(int val) { this.val = val; } - * TreeNode(int val, TreeNode left, TreeNode right) { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { * this.val = val; * this.left = left; * this.right = right; * } * } */ -class Solution { - public TreeNode invertTree(TreeNode root) { +public class Solution { + public TreeNode InvertTree(TreeNode root) { if (root == null) { return null; } - TreeNode l = invertTree(root.left); - TreeNode r = invertTree(root.right); + TreeNode l = InvertTree(root.left); + TreeNode r = InvertTree(root.right); root.left = r; root.right = l; return root; @@ -350,112 +294,6 @@ class Solution { } ``` -#### C++ - -```cpp -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode() : val(0), left(nullptr), right(nullptr) {} - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} - * }; - */ -class Solution { -public: - TreeNode* invertTree(TreeNode* root) { - if (!root) { - return root; - } - TreeNode* l = invertTree(root->left); - TreeNode* r = invertTree(root->right); - root->left = r; - root->right = l; - return root; - } -}; -``` - -#### Go - -```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func invertTree(root *TreeNode) *TreeNode { - if root == nil { - return root - } - l, r := invertTree(root.Left), invertTree(root.Right) - root.Left, root.Right = r, l - return root -} -``` - -#### TypeScript - -```ts -/** - * Definition for a binary tree node. - * class TreeNode { - * val: number - * left: TreeNode | null - * right: TreeNode | null - * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - * } - */ - -function invertTree(root: TreeNode | null): TreeNode | null { - if (!root) { - return root; - } - const l = invertTree(root.left); - const r = invertTree(root.right); - root.left = r; - root.right = l; - return root; -} -``` - -#### JavaScript - -```js -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -var invertTree = function (root) { - if (!root) { - return root; - } - const l = invertTree(root.left); - const r = invertTree(root.right); - root.left = r; - root.right = l; - return root; -}; -``` - diff --git a/solution/0200-0299/0226.Invert Binary Tree/README_EN.md b/solution/0200-0299/0226.Invert Binary Tree/README_EN.md index eaa496ae32679..a07d1f80c87b2 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/README_EN.md +++ b/solution/0200-0299/0226.Invert Binary Tree/README_EN.md @@ -59,9 +59,9 @@ tags: ### Solution 1: Recursion -The idea of recursion is very simple, which is to swap the left and right subtrees of the current node, and then recursively swap the left and right subtrees of the current node. +First, we check if $\textit{root}$ is null. If it is, we return $\text{null}$. Then, we recursively invert the left and right subtrees, set the inverted right subtree as the new left subtree, and set the inverted left subtree as the new right subtree. Finally, we return $\textit{root}$. -The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. @@ -76,14 +76,10 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is # self.right = right class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - def dfs(root): - if root is None: - return - root.left, root.right = root.right, root.left - dfs(root.left) - dfs(root.right) - - dfs(root) + if root is None: + return None + l, r = self.invertTree(root.left), self.invertTree(root.right) + root.left, root.right = r, l return root ``` @@ -107,19 +103,14 @@ class Solution: */ class Solution { public TreeNode invertTree(TreeNode root) { - dfs(root); - return root; - } - - private void dfs(TreeNode root) { if (root == null) { - return; + return null; } - TreeNode t = root.left; - root.left = root.right; - root.right = t; - dfs(root.left); - dfs(root.right); + TreeNode l = invertTree(root.left); + TreeNode r = invertTree(root.right); + root.left = r; + root.right = l; + return root; } } ``` @@ -141,15 +132,13 @@ class Solution { class Solution { public: TreeNode* invertTree(TreeNode* root) { - function dfs = [&](TreeNode* root) { - if (!root) { - return; - } - swap(root->left, root->right); - dfs(root->left); - dfs(root->right); - }; - dfs(root); + if (!root) { + return root; + } + TreeNode* l = invertTree(root->left); + TreeNode* r = invertTree(root->right); + root->left = r; + root->right = l; return root; } }; @@ -167,16 +156,11 @@ public: * } */ func invertTree(root *TreeNode) *TreeNode { - var dfs func(*TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - root.Left, root.Right = root.Right, root.Left - dfs(root.Left) - dfs(root.Right) + if root == nil { + return root } - dfs(root) + l, r := invertTree(root.Left), invertTree(root.Right) + root.Left, root.Right = r, l return root } ``` @@ -199,15 +183,13 @@ func invertTree(root *TreeNode) *TreeNode { */ function invertTree(root: TreeNode | null): TreeNode | null { - const dfs = (root: TreeNode | null) => { - if (root === null) { - return; - } - [root.left, root.right] = [root.right, root.left]; - dfs(root.left); - dfs(root.right); - }; - dfs(root); + if (!root) { + return root; + } + const l = invertTree(root.left); + const r = invertTree(root.right); + root.left = r; + root.right = l; return root; } ``` @@ -233,23 +215,17 @@ function invertTree(root: TreeNode | null): TreeNode | null { // } // } // } -use std::cell::RefCell; use std::rc::Rc; +use std::cell::RefCell; impl Solution { - #[allow(dead_code)] pub fn invert_tree(root: Option>>) -> Option>> { - if root.is_none() { - return root; + if let Some(node) = root.clone() { + let mut node = node.borrow_mut(); + let left = node.left.take(); + let right = node.right.take(); + node.left = Self::invert_tree(right); + node.right = Self::invert_tree(left); } - let left = root.as_ref().unwrap().borrow().left.clone(); - let right = root.as_ref().unwrap().borrow().right.clone(); - // Invert the subtree - let inverted_left = Self::invert_tree(right); - let inverted_right = Self::invert_tree(left); - // Update the left & right - root.as_ref().unwrap().borrow_mut().left = inverted_left; - root.as_ref().unwrap().borrow_mut().right = inverted_right; - // Return the root root } } @@ -271,72 +247,40 @@ impl Solution { * @return {TreeNode} */ var invertTree = function (root) { - const dfs = root => { - if (!root) { - return; - } - [root.left, root.right] = [root.right, root.left]; - dfs(root.left); - dfs(root.right); - }; - dfs(root); + if (!root) { + return root; + } + const l = invertTree(root.left); + const r = invertTree(root.right); + root.left = r; + root.right = l; return root; }; ``` - - - +#### C# - - -### Solution 2 - - - -#### Python3 - -```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -class Solution: - def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - if root is None: - return None - l, r = self.invertTree(root.left), self.invertTree(root.right) - root.left, root.right = r, l - return root -``` - -#### Java - -```java +```cs /** * Definition for a binary tree node. * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode() {} - * TreeNode(int val) { this.val = val; } - * TreeNode(int val, TreeNode left, TreeNode right) { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { * this.val = val; * this.left = left; * this.right = right; * } * } */ -class Solution { - public TreeNode invertTree(TreeNode root) { +public class Solution { + public TreeNode InvertTree(TreeNode root) { if (root == null) { return null; } - TreeNode l = invertTree(root.left); - TreeNode r = invertTree(root.right); + TreeNode l = InvertTree(root.left); + TreeNode r = InvertTree(root.right); root.left = r; root.right = l; return root; @@ -344,112 +288,6 @@ class Solution { } ``` -#### C++ - -```cpp -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode() : val(0), left(nullptr), right(nullptr) {} - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} - * }; - */ -class Solution { -public: - TreeNode* invertTree(TreeNode* root) { - if (!root) { - return root; - } - TreeNode* l = invertTree(root->left); - TreeNode* r = invertTree(root->right); - root->left = r; - root->right = l; - return root; - } -}; -``` - -#### Go - -```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func invertTree(root *TreeNode) *TreeNode { - if root == nil { - return root - } - l, r := invertTree(root.Left), invertTree(root.Right) - root.Left, root.Right = r, l - return root -} -``` - -#### TypeScript - -```ts -/** - * Definition for a binary tree node. - * class TreeNode { - * val: number - * left: TreeNode | null - * right: TreeNode | null - * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - * } - */ - -function invertTree(root: TreeNode | null): TreeNode | null { - if (!root) { - return root; - } - const l = invertTree(root.left); - const r = invertTree(root.right); - root.left = r; - root.right = l; - return root; -} -``` - -#### JavaScript - -```js -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -var invertTree = function (root) { - if (!root) { - return root; - } - const l = invertTree(root.left); - const r = invertTree(root.right); - root.left = r; - root.right = l; - return root; -}; -``` - diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.cpp b/solution/0200-0299/0226.Invert Binary Tree/Solution.cpp index d805b989c0d1a..b38cbe84aa21a 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.cpp +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.cpp @@ -12,15 +12,13 @@ class Solution { public: TreeNode* invertTree(TreeNode* root) { - function dfs = [&](TreeNode* root) { - if (!root) { - return; - } - swap(root->left, root->right); - dfs(root->left); - dfs(root->right); - }; - dfs(root); + if (!root) { + return root; + } + TreeNode* l = invertTree(root->left); + TreeNode* r = invertTree(root->right); + root->left = r; + root->right = l; return root; } }; \ No newline at end of file diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.cs b/solution/0200-0299/0226.Invert Binary Tree/Solution.cs new file mode 100644 index 0000000000000..89fa1f4395098 --- /dev/null +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.cs @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + public TreeNode InvertTree(TreeNode root) { + if (root == null) { + return null; + } + TreeNode l = InvertTree(root.left); + TreeNode r = InvertTree(root.right); + root.left = r; + root.right = l; + return root; + } +} diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.go b/solution/0200-0299/0226.Invert Binary Tree/Solution.go index 05a56cde015fa..cc5dd1fe6ddcf 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.go +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.go @@ -7,15 +7,10 @@ * } */ func invertTree(root *TreeNode) *TreeNode { - var dfs func(*TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - root.Left, root.Right = root.Right, root.Left - dfs(root.Left) - dfs(root.Right) + if root == nil { + return root } - dfs(root) + l, r := invertTree(root.Left), invertTree(root.Right) + root.Left, root.Right = r, l return root } \ No newline at end of file diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.java b/solution/0200-0299/0226.Invert Binary Tree/Solution.java index 914dbc3ee2011..fd6036ec7a765 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.java +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.java @@ -15,18 +15,13 @@ */ class Solution { public TreeNode invertTree(TreeNode root) { - dfs(root); - return root; - } - - private void dfs(TreeNode root) { if (root == null) { - return; + return null; } - TreeNode t = root.left; - root.left = root.right; - root.right = t; - dfs(root.left); - dfs(root.right); + TreeNode l = invertTree(root.left); + TreeNode r = invertTree(root.right); + root.left = r; + root.right = l; + return root; } } \ No newline at end of file diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.js b/solution/0200-0299/0226.Invert Binary Tree/Solution.js index 9d24a4bfda480..ab4248be39f48 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.js +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.js @@ -11,14 +11,12 @@ * @return {TreeNode} */ var invertTree = function (root) { - const dfs = root => { - if (!root) { - return; - } - [root.left, root.right] = [root.right, root.left]; - dfs(root.left); - dfs(root.right); - }; - dfs(root); + if (!root) { + return root; + } + const l = invertTree(root.left); + const r = invertTree(root.right); + root.left = r; + root.right = l; return root; }; diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.py b/solution/0200-0299/0226.Invert Binary Tree/Solution.py index adabf8a20411a..4bf14d65fa36c 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.py +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.py @@ -6,12 +6,8 @@ # self.right = right class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - def dfs(root): - if root is None: - return - root.left, root.right = root.right, root.left - dfs(root.left) - dfs(root.right) - - dfs(root) + if root is None: + return None + l, r = self.invertTree(root.left), self.invertTree(root.right) + root.left, root.right = r, l return root diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.rs b/solution/0200-0299/0226.Invert Binary Tree/Solution.rs index 58f8a78966e30..5e08909fd2f73 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.rs +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.rs @@ -19,20 +19,14 @@ use std::cell::RefCell; use std::rc::Rc; impl Solution { - #[allow(dead_code)] pub fn invert_tree(root: Option>>) -> Option>> { - if root.is_none() { - return root; + if let Some(node) = root.clone() { + let mut node = node.borrow_mut(); + let left = node.left.take(); + let right = node.right.take(); + node.left = Self::invert_tree(right); + node.right = Self::invert_tree(left); } - let left = root.as_ref().unwrap().borrow().left.clone(); - let right = root.as_ref().unwrap().borrow().right.clone(); - // Invert the subtree - let inverted_left = Self::invert_tree(right); - let inverted_right = Self::invert_tree(left); - // Update the left & right - root.as_ref().unwrap().borrow_mut().left = inverted_left; - root.as_ref().unwrap().borrow_mut().right = inverted_right; - // Return the root root } } diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.ts b/solution/0200-0299/0226.Invert Binary Tree/Solution.ts index 72c1b6276d2f7..d87a5413b484a 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.ts +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.ts @@ -13,14 +13,12 @@ */ function invertTree(root: TreeNode | null): TreeNode | null { - const dfs = (root: TreeNode | null) => { - if (root === null) { - return; - } - [root.left, root.right] = [root.right, root.left]; - dfs(root.left); - dfs(root.right); - }; - dfs(root); + if (!root) { + return root; + } + const l = invertTree(root.left); + const r = invertTree(root.right); + root.left = r; + root.right = l; return root; } diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution2.cpp b/solution/0200-0299/0226.Invert Binary Tree/Solution2.cpp deleted file mode 100644 index b38cbe84aa21a..0000000000000 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution2.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode() : val(0), left(nullptr), right(nullptr) {} - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} - * }; - */ -class Solution { -public: - TreeNode* invertTree(TreeNode* root) { - if (!root) { - return root; - } - TreeNode* l = invertTree(root->left); - TreeNode* r = invertTree(root->right); - root->left = r; - root->right = l; - return root; - } -}; \ No newline at end of file diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution2.go b/solution/0200-0299/0226.Invert Binary Tree/Solution2.go deleted file mode 100644 index cc5dd1fe6ddcf..0000000000000 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution2.go +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func invertTree(root *TreeNode) *TreeNode { - if root == nil { - return root - } - l, r := invertTree(root.Left), invertTree(root.Right) - root.Left, root.Right = r, l - return root -} \ No newline at end of file diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution2.java b/solution/0200-0299/0226.Invert Binary Tree/Solution2.java deleted file mode 100644 index fd6036ec7a765..0000000000000 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution2.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Definition for a binary tree node. - * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode() {} - * TreeNode(int val) { this.val = val; } - * TreeNode(int val, TreeNode left, TreeNode right) { - * this.val = val; - * this.left = left; - * this.right = right; - * } - * } - */ -class Solution { - public TreeNode invertTree(TreeNode root) { - if (root == null) { - return null; - } - TreeNode l = invertTree(root.left); - TreeNode r = invertTree(root.right); - root.left = r; - root.right = l; - return root; - } -} \ No newline at end of file diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution2.js b/solution/0200-0299/0226.Invert Binary Tree/Solution2.js deleted file mode 100644 index ab4248be39f48..0000000000000 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution2.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -var invertTree = function (root) { - if (!root) { - return root; - } - const l = invertTree(root.left); - const r = invertTree(root.right); - root.left = r; - root.right = l; - return root; -}; diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution2.py b/solution/0200-0299/0226.Invert Binary Tree/Solution2.py deleted file mode 100644 index 4bf14d65fa36c..0000000000000 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution2.py +++ /dev/null @@ -1,13 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -class Solution: - def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - if root is None: - return None - l, r = self.invertTree(root.left), self.invertTree(root.right) - root.left, root.right = r, l - return root diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution2.ts b/solution/0200-0299/0226.Invert Binary Tree/Solution2.ts deleted file mode 100644 index d87a5413b484a..0000000000000 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution2.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Definition for a binary tree node. - * class TreeNode { - * val: number - * left: TreeNode | null - * right: TreeNode | null - * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - * } - */ - -function invertTree(root: TreeNode | null): TreeNode | null { - if (!root) { - return root; - } - const l = invertTree(root.left); - const r = invertTree(root.right); - root.left = r; - root.right = l; - return root; -}