Merge pull request #1127 from xiaofei-2020/tree27

添加(0236.二叉树的最近公共祖先.md):增加typescript版本
This commit is contained in:
程序员Carl 2022-03-19 10:03:53 +08:00 committed by GitHub
commit 584311a478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -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;
};
```
-----------------------