From 93ab9befe85fdd3056765b3414b8b442a7f2258d Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:51:02 +1300 Subject: [PATCH 1/3] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20=E5=A2=9E=E5=8A=A0C#=E5=8D=95?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index d89bf44b..6900e668 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -1046,6 +1046,8 @@ class MyStack() { ### C#: +> 双队列 + ```csharp public class MyStack { Queue queue1; @@ -1080,6 +1082,54 @@ public class MyStack { } ``` +> 单队列 + +```c# +/* + * @lc app=leetcode id=225 lang=csharp + * 版本二:单队列 + * [225] Implement Stack using Queues + */ + +// @lc code=start +public class MyStack { + Queue myQueue; + public MyStack() { + myQueue = new Queue(); + } + + public void Push(int x) { + myQueue.Enqueue(x); + } + + //使用一个队列实现 + public int Pop() { + //一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。 + for (var i = 0; i < myQueue.Count-1; i++) + { + myQueue.Enqueue(myQueue.Dequeue()); + } + return myQueue.Dequeue(); + } + + //复用Pop()的代码 + public int Top() { + int res = Pop(); + myQueue.Enqueue(res); + return res; + } + + public bool Empty() { + return (myQueue.Count == 0); + } +} + +// @lc code=end + +``` + + + ### PHP: > 双队列 @@ -1203,3 +1253,4 @@ impl MyStack { + From 650e33bc709f59df007625ff524906c4bd59585c Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Thu, 11 Jan 2024 14:50:59 +1300 Subject: [PATCH 2/3] =?UTF-8?q?update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20=E5=A2=9E=E5=8A=A0C#?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树理论基础.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index e2c6d83c..50d592a2 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -302,9 +302,19 @@ impl TreeNode { } } } -``` +``` +```c# +public class TreeNode +{ + public int val; + public TreeNode left; + public TreeNode right; + public TreeNode(int x) { val = x; } +} +```

+ From b13487089cb782412c156836be0dc00fef53d1da Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Thu, 11 Jan 2024 23:41:09 +1300 Subject: [PATCH 3/3] =?UTF-8?q?update=200116.=E5=A1=AB=E5=85=85=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88.md=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0C#=E8=BF=AD=E4=BB=A3=E5=92=8C=E9=80=92?= =?UTF-8?q?=E5=BD=92=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...个节点的下一个右侧节点指针.md | 77 +++++++++++++++++++ problems/二叉树的递归遍历.md | 1 + 2 files changed, 78 insertions(+) diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 003ef75a..60ea9210 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -358,7 +358,84 @@ function connect(root: NodePro | null): NodePro | null { }; ``` +```csharp +//递归 +public class Solution { + public Node Connect(Node root) { + if (root == null) { + return null; + } + + ConnectNodes(root.left, root.right); + + return root; + } + private void ConnectNodes(Node node1, Node node2) { + if (node1 == null || node2 == null) { + return; + } + + // 将左子节点的 next 指向右子节点 + node1.next = node2; + + // 递归连接当前节点的左右子节点 + ConnectNodes(node1.left, node1.right); + ConnectNodes(node2.left, node2.right); + + // 连接跨越父节点的两个子树 + ConnectNodes(node1.right, node2.left); + } +} + + +// 迭代 +public class Solution +{ + public Node Connect(Node root) + { + Queue que = new Queue(); + + if (root != null) + { + que.Enqueue(root); + } + + while (que.Count > 0) + { + + var queSize = que.Count; + for (int i = 0; i < queSize; i++) + { + var cur = que.Dequeue(); + + // 当这个节点不是这一层的最后的节点 + if (i != queSize - 1) + { + // 当前节点指向下一个节点 + cur.next = que.Peek(); + } + // 否则指向空 + else + { + cur.next = null; + } + + if (cur.left != null) + { + que.Enqueue(cur.left); + } + if (cur.right != null) + { + que.Enqueue(cur.right); + } + } + } + + return root; + } +} +```

diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 6dad6e56..b6dd7b6f 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -623,3 +623,4 @@ public void Traversal(TreeNode cur, IList res) +