-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b51843
commit 6a04a63
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/page-2/108. Convert Sorted Array to Binary Search Tree/sortedArrayToBST.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { stringify } from 'flatted'; | ||
|
||
import { generateBinaryTree } from '~/utils/binary-tree'; | ||
|
||
import { sortedArrayToBST } from './sortedArrayToBST'; | ||
|
||
describe('110. Balanced Binary Tree', () => { | ||
it('sortedArrayToBST - Case 1', () => { | ||
const expected = generateBinaryTree([0, -10, 5, null, -3, null, 9]); | ||
expect(stringify(sortedArrayToBST([-10, -3, 0, 5, 9]))).toBe(stringify(expected)); | ||
}); | ||
|
||
it('sortedArrayToBST - Case 2', () => { | ||
const expected = generateBinaryTree([1, null, 3]); | ||
expect(stringify(sortedArrayToBST([1, 3]))).toBe(stringify(expected)); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/page-2/108. Convert Sorted Array to Binary Search Tree/sortedArrayToBST.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { TreeNode } from '~/utils/binary-tree'; | ||
|
||
type SortedArrayToBST = (nums: number[]) => TreeNode | null; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const sortedArrayToBST: SortedArrayToBST = (nums) => { | ||
if (nums.length === 0) return null; | ||
|
||
function convertToBST(left: number, right: number): TreeNode | null { | ||
if (left > right) return null; | ||
|
||
const mid = Math.floor((left + right) / 2); | ||
const node = new TreeNode(nums[mid]); | ||
|
||
node.left = convertToBST(left, mid - 1); | ||
node.right = convertToBST(mid + 1, right); | ||
|
||
return node; | ||
} | ||
|
||
return convertToBST(0, nums.length - 1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { generateBinaryTree } from '~/utils/binary-tree'; | ||
|
||
import { isBalanced } from './isBalanced'; | ||
|
||
describe('110. Balanced Binary Tree', () => { | ||
it('isBalanced - Case 1', () => { | ||
// https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg | ||
const root = generateBinaryTree([3, 9, 20, null, null, 15, 7]); | ||
expect(isBalanced(root)).toBe(true); | ||
}); | ||
|
||
it('isBalanced - Case 2', () => { | ||
// https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg | ||
const root = generateBinaryTree([1, 2, 2, 3, 3, null, null, 4, 4]); | ||
expect(isBalanced(root)).toBe(false); | ||
}); | ||
|
||
it('isBalanced - Case 3', () => { | ||
const root = generateBinaryTree([]); | ||
expect(isBalanced(root)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { TreeNode } from '~/utils/binary-tree'; | ||
|
||
type IsBalanced = (root: TreeNode | null) => boolean; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const isBalanced: IsBalanced = (root) => { | ||
function checkHeight(node: TreeNode | null): number { | ||
if (node === null) return 0; | ||
|
||
const leftHeight = checkHeight(node.left); | ||
const rightHeight = checkHeight(node.right); | ||
|
||
// If left subtree is unbalanced or right subtree is unbalanced | ||
// or if the height difference is more than 1, return -1 indicating unbalanced | ||
if (leftHeight === -1 || rightHeight === -1 || Math.abs(leftHeight - rightHeight) > 1) { | ||
return -1; | ||
} | ||
|
||
// Return the height of the current node | ||
// which is 1 plus the maximum height of its left and right subtrees | ||
return Math.max(leftHeight, rightHeight) + 1; | ||
} | ||
|
||
// The tree is balanced if checkHeight does not return -1 | ||
return checkHeight(root) !== -1; | ||
}; |