Update0236.二叉树的最近公共祖先,提交C#版

This commit is contained in:
eeee0717
2023-12-04 09:42:53 +08:00
parent 0ed1134870
commit a0e2c5ceb0

View File

@ -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;
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">