Skip to content

Commit

Permalink
134th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 2, 2024
1 parent 6b51843 commit 6a04a63
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
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));
});
});
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);
};
22 changes: 22 additions & 0 deletions src/page-2/110. Balanced Binary Tree/isBalanced.spec.ts
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);
});
});
28 changes: 28 additions & 0 deletions src/page-2/110. Balanced Binary Tree/isBalanced.ts
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;
};

0 comments on commit 6a04a63

Please sign in to comment.