mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加(0236.二叉树的最近公共祖先.md):增加typescript版本
This commit is contained in:
@ -325,6 +325,20 @@ var lowestCommonAncestor = function(root, p, q) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
|
||||
if (root === null || root === p || root === q) return root;
|
||||
const left = lowestCommonAncestor(root.left, p, q);
|
||||
const right = lowestCommonAncestor(root.right, p, q);
|
||||
if (left !== null && right !== null) return root;
|
||||
if (left !== null) return left;
|
||||
if (right !== null) return right;
|
||||
return null;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user