[CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树

4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.

这道题给了我们一个有序的数组,让我们来生成一个最小高度的二叉搜索树,为了达到最小高度,肯定是尽可能的填成一个满二叉树,左子树填满,右子树尽可能的填满。而且要注意是二叉搜索树,左<根<右的性质不能忘。既然给了我们一个有序的数组,那么我们可以取中间的数字为根节点,然后左半段为左子树,右半段为右子树,然后再递归去分别再分,有点像二叉搜索法的原理,代码不复杂,也不难懂,如下所示:

class Solution {
public:
    TreeNode* createMinimalBST(vector<int> &nums) {
        return createMinimalBST(nums, 0, nums.size() - 1);
    }
    TreeNode* createMinimalBST(vector<int> &nums, int start, int end) {
        if (start > end) return NULL;
        int mid = (start + end) / 2;
        TreeNode *node = new TreeNode(nums[mid]);
        node->left = createMinimalBST(nums, start, mid - 1);
        node->right = createMinimalBST(nums, mid + 1, end);
        return node;
    }
};
时间: 2024-10-11 22:56:28

[CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树的相关文章

【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】

[098-Validate Binary Search Tree(验证二叉搜索树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys le

LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 首先是O(N)空间的方法,用递归: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *ri

LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / /

[LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed to have only one unique value in the BST that is closest to the target.

? leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

[LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}&

LeetCode 96 Unique Binary Search Trees不同的二叉搜索树的个数

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 1 class Solution { 2 public: 3 int numTrees(int n) { 4 int *dp = new int[n+1]; 5 for(int i=0;i<n+1;++i) 6 dp[i] = 0; 7 8 dp[0] = 1;//!!! 9 for(int sz = 1; sz <

LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 一开始是想用递归解决,但看了标签是dp问题,就想了一下, 数目为k的bst,其每个 0 ~ k - 1