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

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