From dba4e9d00ec28ea484715bd5fa341ae8aed9ed98 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 15 Nov 2023 08:57:19 +0800 Subject: [PATCH] =?UTF-8?q?Update226.=E7=BF=BB=E8=BD=AC=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=EF=BC=8C=E7=AE=80=E5=8C=96C#=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) 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; +} } ```