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

This commit is contained in:
2020-05-10 15:31:25 +08:00
12 changed files with 223 additions and 2 deletions

View File

@ -0,0 +1,15 @@
import { maximalSquare } from '../../src/array/maximal-square'
test('最大正方形', () => {
expect(maximalSquare([
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 0]
])).toBe(4)
expect(maximalSquare([
[0, 0, 0],
[1, 1, 1]
])).toBe(1)
})

View File

@ -0,0 +1,6 @@
import { mincostTickets } from '../../src/array/minimum-cost-for-tickets'
test('最低票价', () => {
expect(mincostTickets([1, 4, 6, 7, 8, 20], [2, 7, 15])).toBe(11)
expect(mincostTickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 7, 15])).toBe(17)
})

View File

@ -0,0 +1,24 @@
import { hasCycle } from '../../src/list/linked-list-cycle'
function ListNode (val) {
this.val = val
this.next = null
}
const arrToList = (arr) => {
const head = new ListNode(arr[0])
let current = head
for (let n = 1, len = arr.length; n < len; n++) {
current.next = new ListNode(arr[n])
current = current.next
}
return head
}
test('环形链表', () => {
const list = arrToList([1, 2, 3])
list.next.next.next = list
expect(hasCycle(list)).toBe(true)
expect(hasCycle(arrToList([1, 2, 3]))).toBe(false)
})

View File

@ -0,0 +1,6 @@
import { reverseLeftWords } from '../../src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof'
test('左旋转字符串', () => {
expect(reverseLeftWords('abcdefg', 2)).toBe('cdefgab')
expect(reverseLeftWords('lrloseumgh', 6)).toBe('umghlrlose')
})

View File

@ -0,0 +1,7 @@
import { isSubtree } from '../../src/tree/subtree-of-another-tree'
import Tree from './Tree'
test('另一个树的子树', () => {
expect(isSubtree(Tree.arrToTree([3, 4, 5, 1, 2]), Tree.arrToTree([4, 1, 2]))).toBe(true)
expect(isSubtree(Tree.arrToTree([3, 4, 5, 1, 2, null, null, null, null, 0]), Tree.arrToTree([4, 1, 2]))).toBe(false)
})