From a0e2c5ceb06d9199bc6f2ce2eb0aa87a8ebf8fe4 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 4 Dec 2023 09:42:53 +0800 Subject: [PATCH] =?UTF-8?q?Update0236.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E4=BA=A4C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0236.二叉树的最近公共祖先.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index edb5ea3e..0e99f1c2 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -431,6 +431,19 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + if (root == null || root == p || root == q) return root; + TreeNode left = LowestCommonAncestor(root.left, p, q); + TreeNode right = LowestCommonAncestor(root.right, p, q); + if (left != null && right != null) return root; + if (left == null && right != null) return right; + if (left != null && right == null) return left; + return null; +} +```