From 8945fd98094efe154431cb51a7edf01e3b50af60 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 11 Nov 2023 09:12:17 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Update.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的递归遍历.md | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 730b18ac..6dad6e56 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -565,6 +565,60 @@ impl Solution { } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; + +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + res.Add(cur.val); + Traversal(cur.left, res); + Traversal(cur.right, res); +} +``` +```C# +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + Traversal(cur.left, res); + res.Add(cur.val); + Traversal(cur.right, res); +} +``` +```C# +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + Traversal(cur.left, res); + Traversal(cur.right, res); + res.Add(cur.val); +} +``` +

From 5d9d37f01d59b2fa95001018fb24e4b74e2a04f9 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 12 Nov 2023 09:15:59 +0800 Subject: [PATCH 2/7] =?UTF-8?q?Update.=E4=BA=8C=E5=8F=89=E6=A0=91=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E9=81=8D=E5=8E=86=EF=BC=8CC#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的迭代遍历.md | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index 69007995..e39709b8 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -695,6 +695,67 @@ impl Solution { } } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var st = new Stack(); + var res = new List(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Pop(); + res.Add(node.val); + if (node.right != null) + st.Push(node.right); + if (node.left != null) + st.Push(node.left); + } + return res; +} + +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var st = new Stack(); + var res = new List(); + var cur = root; + while (st.Count != 0 || cur != null) + { + if (cur != null) + { + st.Push(cur); + cur = cur.left; + } + else + { + cur = st.Pop(); + res.Add(cur.val); + cur = cur.right; + } + } + return res; +} +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var cur = st.Pop(); + res.Add(cur.val); + if (cur.left != null) st.Push(cur.left); + if (cur.right != null) st.Push(cur.right); + } + res.Reverse(0, res.Count()); + return res; +} +```

From 436bd5a1a25d317824af0405bba0d023a7619a16 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 13 Nov 2023 09:21:04 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Update.=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 93 +++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 8089af64..ad79424d 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -741,6 +741,99 @@ impl Solution{ } } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Peek(); + if (node == null) + { + st.Pop(); + node = st.Peek(); + st.Pop(); + res.Add(node.val); + } + else + { + st.Pop(); + if (node.right != null) st.Push(node.right); + if (node.left != null) st.Push(node.left); + st.Push(node); + st.Push(null); + } + } + return res; +} +``` +```C# +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Peek(); + if (node == null) + { + st.Pop(); + node = st.Peek(); + st.Pop(); + res.Add(node.val); + } + else + { + st.Pop(); + if (node.right != null) st.Push(node.right); + st.Push(node); + st.Push(null); + if (node.left != null) st.Push(node.left); + } + } + return res; +} +``` + +```C# +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Peek(); + if (node == null) + { + st.Pop(); + node = st.Peek(); + st.Pop(); + res.Add(node.val); + } + else + { + st.Pop(); + if (node.left != null) st.Push(node.left); + if (node.right != null) st.Push(node.right); + st.Push(node); + st.Push(null); + } + } + res.Reverse(0, res.Count); + return res; +} +```

From aad5029a1cb33461905df368c951bceeb2fbc797 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 14 Nov 2023 09:11:17 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Update=20102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 51d030e7..e977279c 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -462,6 +462,32 @@ impl Solution { } } ``` +### C# +```C# +public IList> LevelOrder(TreeNode root) +{ + var res = new List>(); + var que = new Queue(); + if (root == null) return res; + que.Enqueue(root); + while (que.Count != 0) + { + var size = que.Count; + var vec = new List(); + for (int i = 0; i < size; i++) + { + var cur = que.Dequeue(); + vec.Add(cur.val); + if (cur.left != null) que.Enqueue(cur.left); + if (cur.right != null) que.Enqueue(cur.right); + } + res.Add(vec); + + + } + return res; +} +``` **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** @@ -798,6 +824,31 @@ impl Solution { } } ``` +### C# +```C# +public IList> LevelOrderBottom(TreeNode root) +{ + var res = new List>(); + var que = new Queue(); + if (root == null) return res; + que.Enqueue(root); + while (que.Count != 0) + { + var size = que.Count; + var vec = new List(); + for (int i = 0; i < size; i++) + { + var cur = que.Dequeue(); + vec.Add(cur.val); + if (cur.left != null) que.Enqueue(cur.left); + if (cur.right != null) que.Enqueue(cur.right); + } + res.Add(vec); + } + res.Reverse(); + return res; +} +``` ## 199.二叉树的右视图 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 5/7] =?UTF-8?q?Update226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E7=AE=80=E5=8C=96C#=E4=BB=A3?= =?UTF-8?q?=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; +} } ``` From b51573efa69a3e4fead79bbcba62c77f7ecedb09 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 15 Nov 2023 09:16:04 +0800 Subject: [PATCH 6/7] =?UTF-8?q?Update101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 36b39740..1b777dd5 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -897,6 +897,53 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public bool IsSymmetric(TreeNode root) +{ + if (root == null) return true; + return Compare(root.left, root.right); +} +public bool Compare(TreeNode left, TreeNode right) +{ + if(left == null && right != null) return false; + else if(left != null && right == null ) return false; + else if(left == null && right == null) return true; + else if(left.val != right.val) return false; + + var outside = Compare(left.left, right.right); + var inside = Compare(left.right, right.left); + + return outside&&inside; +} +``` +``` C# +// 迭代法 +public bool IsSymmetric(TreeNode root) +{ + if (root == null) return true; + var st = new Stack(); + st.Push(root.left); + st.Push(root.right); + while (st.Count != 0) + { + var left = st.Pop(); + var right = st.Pop(); + if (left == null && right == null) + continue; + + if ((left == null || right == null || (left.val != right.val))) + return false; + + st.Push(left.left); + st.Push(right.right); + st.Push(left.right); + st.Push(right.left); + } + return true; +} +```

From 3ffca338cafa5ba72d81048d762211488f7d19f7 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 16 Nov 2023 09:12:06 +0800 Subject: [PATCH 7/7] =?UTF-8?q?Update104.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 05044375..ab3ce52e 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -1022,6 +1022,61 @@ impl Solution { max_depth } ``` +### C# +```C# +// 递归法 +public int MaxDepth(TreeNode root) { + if(root == null) return 0; + + int leftDepth = MaxDepth(root.left); + int rightDepth = MaxDepth(root.right); + + return 1 + Math.Max(leftDepth, rightDepth); +} +``` +```C# +// 前序遍历 +int result = 0; +public int MaxDepth(TreeNode root) +{ + if (root == null) return result; + GetDepth(root, 1); + return result; +} +public void GetDepth(TreeNode root, int depth) +{ + result = depth > result ? depth : result; + if (root.left == null && root.right == null) return; + + if (root.left != null) + GetDepth(root.left, depth + 1); + if (root.right != null) + GetDepth(root.right, depth + 1); + return; +} +``` +```C# +// 迭代法 +public int MaxDepth(TreeNode root) +{ + int depth = 0; + Queue que = new(); + if (root == null) return depth; + que.Enqueue(root); + while (que.Count != 0) + { + int size = que.Count; + depth++; + for (int i = 0; i < size; i++) + { + var node = que.Dequeue(); + if (node.left != null) que.Enqueue(node.left); + if (node.right != null) que.Enqueue(node.right); + } + } + return depth; +} +```