mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update0236.二叉树的最近公共祖先,提交C#版
This commit is contained in:
@ -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">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user