From 09e230bef32642e1e00f6223b83749a93989d70c Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 5 Dec 2023 09:08:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update0235.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.二叉搜索树的最近公共祖先.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index b7e92e4e..b27a231e 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -513,6 +513,31 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + if (root.val > p.val && root.val > q.val) + return LowestCommonAncestor(root.left, p, q); + if (root.val < p.val && root.val < q.val) + return LowestCommonAncestor(root.right, p, q); + return root; +} +// 迭代 +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + while (root != null) + { + if (root.val > p.val && root.val > q.val) + root = root.left; + else if (root.val < p.val && root.val < q.val) + root = root.right; + else return root; + } + return null; +} +```

From 35322cb120cd2d64e1e760eb6662fa68dded9e2b Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 6 Dec 2023 09:05:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update0701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0701.二叉搜索树中的插入操作.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 265bbb5b..98e60d5f 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -691,6 +691,17 @@ impl Solution { } } ``` +### C# +``` C# +// 递归 +public TreeNode InsertIntoBST(TreeNode root, int val) { + if (root == null) return new TreeNode(val); + + if (root.val > val) root.left = InsertIntoBST(root.left, val); + if (root.val < val) root.right = InsertIntoBST(root.right, val); + return root; +} +```

From 7692d6cc97a999016aa0bd023433a2e0975e262d Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 8 Dec 2023 08:08:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0669.修剪二叉搜索树.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 00186558..229a2ab1 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -567,6 +567,23 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public TreeNode TrimBST(TreeNode root, int low, int high) +{ + if (root == null) return null; + if (root.val < low) + return TrimBST(root.right, low, high); + + if (root.val > high) + return TrimBST(root.left, low, high); + + root.left = TrimBST(root.left, low, high); + root.right = TrimBST(root.right, low, high); + return root; +} +```