diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 11517783..8691953a 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -1018,25 +1018,13 @@ public class Solution { ```csharp //迭代 public class Solution { - public TreeNode InvertTree(TreeNode root) { - if (root == null) return null; - Stack stack=new Stack(); - stack.Push(root); - while(stack.Count>0) - { - TreeNode node = stack.Pop(); - swap(node); - if(node.right!=null) stack.Push(node.right); - if(node.left!=null) stack.Push(node.left); - } - return root; - } - - public void swap(TreeNode node) { - TreeNode temp = node.left; - node.left = node.right; - node.right = temp; - } +public TreeNode InvertTree(TreeNode root) { + if(root == null) return root; + (root.left,root.right) = (root.right, root.left); + InvertTree(root.left); + InvertTree(root.right); + return root; +} } ```