Update0236.二叉树的最近公共祖先,提交C#版
This commit is contained in:
parent
0ed1134870
commit
a0e2c5ceb0
|
|
@ -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">
|
||||
|
|
|
|||
Loading…
Reference in New Issue