From 29eb6f680f73bcee4d7039864be8bf1d58179adf Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Fri, 12 Jan 2024 21:45:48 +1300 Subject: [PATCH 1/3] =?UTF-8?q?update=200104.=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.md:=20Add=20559.n?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index f4c6cfc8..1f55f197 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -1033,6 +1033,9 @@ impl Solution { } ``` ### C# + +0104.二叉树的最大深度 + ```csharp // 递归法 public int MaxDepth(TreeNode root) { @@ -1088,7 +1091,76 @@ public int MaxDepth(TreeNode root) } ``` +559.n叉树的最大深度 +递归法 +```csharp + /* + 递归法 + */ + public class Solution { + public int MaxDepth(Node root) { + int res = 0; + /* 终止条件 */ + if(root == null){ + return 0; + } + + /* logic */ + // 遍历当前节点的子节点 + for (int i = 0; i < root.children.Count; i++) + { + res = Math.Max(res, MaxDepth(root.children[i])); + } + return res + 1; + } + } + // @lc code=end +``` + 迭代法(层序遍历) +```csharp + /* + 迭代法 + */ + public class Solution + { + public int MaxDepth(Node root) + { + Queue que = new Queue(); // 使用泛型队列存储节点 + + int res = 0; + + if(root != null){ + que.Enqueue(root); // 将根节点加入队列 + } + while (que.Count > 0) + { + int size = que.Count; // 获取当前层的节点数 + res++; // 深度加一 + + for (int i = 0; i < size; i++) + { + // 每一层的遍历 + + var curNode = que.Dequeue(); // 取出队列中的节点 + for (int j = 0; j < curNode.children.Count; j++) + { + if (curNode.children[j] != null) + { + que.Enqueue(curNode.children[j]); // 将子节点加入队列 + } + } + } + } + + return res; // 返回树的最大深度 + + } + } +``` + +

+ From 47688ba24829cab212fa9d3d911199cbe8afee57 Mon Sep 17 00:00:00 2001 From: Chenxue3 <71321839+Chenxue3@users.noreply.github.com> Date: Mon, 15 Jan 2024 23:10:46 +1300 Subject: [PATCH 2/3] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md=20-=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 48 +++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 3cdcc801..d897bba1 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -716,9 +716,55 @@ public void Traversal(TreeNode root, int depth) return; } ``` +```csharp +/* + * @lc app=leetcode id=513 lang=csharp + * 迭代法 + * [513] Find Bottom Left Tree Value + */ + +// @lc code=start +public class Solution +{ + public int FindBottomLeftValue(TreeNode root) + { + Queue que = new Queue(); + + if (root != null) + { + que.Enqueue(root); + } + + int ans = 0; + while (que.Count != 0) + { + + int size = que.Count; + for (var i = 0; i < size; i++) + { + var curNode = que.Peek(); + que.Dequeue(); + if(i == 0){ + ans = curNode.val; + } + if (curNode.left != null) + { + que.Enqueue(curNode.left); + } + if (curNode.right != null) + { + que.Enqueue(curNode.right); + } + } + + } + return ans; + } +} +// @lc code=end +```

- From c6d7c78dee47ee1eded34779c53227c3f49eeef8 Mon Sep 17 00:00:00 2001 From: Chenxue3 <71321839+Chenxue3@users.noreply.github.com> Date: Tue, 16 Jan 2024 01:37:06 +1300 Subject: [PATCH 3/3] =?UTF-8?q?0112.=E8=B7=AF=E5=BE=84=E6=80=BB=E5=92=8C.m?= =?UTF-8?q?d=20=E6=B7=BB=E5=8A=A00113=20=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8CII=20C#=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 28134a7c..d45be3bd 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -1523,8 +1523,64 @@ public bool HasPathSum(TreeNode root, int targetSum) return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val); } ``` +0113.路径总和: +```csharp +/* + * @lc app=leetcode id=113 lang=csharp + * 0113.路径总和 II + * [113] Path Sum II + * 递归法 + */ +public class Solution { + private List> result = new List>(); + private List path = new List(); + // Main function to find paths with the given sum + public IList> PathSum(TreeNode root, int targetSum) { + result.Clear(); + path.Clear(); + if (root == null) return result.Select(list => list as IList).ToList(); + path.Add(root.val); // Add the root node to the path + traversal(root, targetSum - root.val); // Start the traversal + return result.Select(list => list as IList).ToList(); + } + + // Recursive function to traverse the tree and find paths + private void traversal(TreeNode node, int count) { + // If a leaf node is reached and the target sum is achieved + if (node.left == null && node.right == null && count == 0) { + result.Add(new List(path)); // Add a copy of the path to the result + return; + } + + // If a leaf node is reached and the target sum is not achieved, or if it's not a leaf node + if (node.left == null && node.right == null) return; + + // Traverse the left subtree + if (node.left != null) { + path.Add(node.left.val); + count -= node.left.val; + traversal(node.left, count); // Recursive call + count += node.left.val; // Backtrack + path.RemoveAt(path.Count - 1); // Backtrack + } + + // Traverse the right subtree + if (node.right != null) { + path.Add(node.right.val); + count -= node.right.val; + traversal(node.right, count); // Recursive call + count += node.right.val; // Backtrack + path.RemoveAt(path.Count - 1); // Backtrack + } + } +} + +// @lc code=end + +```

+