add: 二叉树的最近公共祖先

This commit is contained in:
2020-05-10 15:30:48 +08:00
parent 1227f993ae
commit 64941ec538
3 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,8 @@
import { lowestCommonAncestor } from '../../src/tree/lowest-common-ancestor-of-a-binary-tree'
import Tree from './Tree'
test('二叉树的最近公共祖先', () => {
const head = Tree.arrToTree([3, 5, 1, 6, 2, 0, 8, null, null, 7, 4])
expect(lowestCommonAncestor(head, head.left, head.right)).toEqual(head)
expect(lowestCommonAncestor(head, head.left, head.left.right.right)).toEqual(head.left)
})