Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2022-10-18 #81

Open
github-actions bot opened this issue Oct 17, 2022 · 1 comment
Open

2022-10-18 #81

github-actions bot opened this issue Oct 17, 2022 · 1 comment

Comments

@github-actions
Copy link

No description provided.

@gongpeione
Copy link
Contributor

108 Convert Sorted Array to Binary Search Tree

/*
 * @lc app=leetcode id=108 lang=typescript
 *
 * [108] Convert Sorted Array to Binary Search Tree
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function sortedArrayToBST(nums: number[]): TreeNode | null {
    const helper = (l, r) => {
        if (l > r) {
            return null;
        }

        const m = ~~((l + r) / 2);
        const node = new TreeNode(
            nums[m],
            helper(l, m - 1),
            helper(m + 1, r)
        );

        return node;
    }

    return helper(0, nums.length - 1);
};
// @lc code=end

Nickname: Geeku
From vscode-hzfe-algorithms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant