zoukankan      html  css  js  c++  java
  • leetcode 108. Convert Sorted Array to Binary Search Tree

    题目描述:

    //用一个升序的数组 构建 高度平衡的儿茶搜索树

    //用递归和二分的思想 

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            return dfs(nums,0,nums.size()-1);
        }
        TreeNode *dfs(vector<int> &nums, int start, int end){
            if(start > end) return NULL;
            int mid = start + (end - start)/2;
            TreeNode *node = new TreeNode(nums[mid]);
            node->left = dfs(nums,start,mid-1);
            node->right = dfs(nums,mid+1,end);
            
            return node;
        }
    };
  • 相关阅读:
    python标准库
    python常用标准库
    django-restframework-serializers
    Resources.UnloadUnusedAssets
    Shader 的 Blend
    C++STL queue
    C++STL stack
    C++STL deque
    C++STL容器重点
    C++STL 迭代器
  • 原文地址:https://www.cnblogs.com/strongYaYa/p/6771062.html
Copyright © 2011-2022 走看看