码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 108

时间:2019-01-30 15:46:16      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:def   bst   color   int   bsp   obs   node   style   tree node   

/**
 * 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 left,int right){
        if(left > right) return NULL;
        int mid = (left+right)/2;
        TreeNode* cur = new TreeNode(nums[mid]);
        cur->left = DFS(nums,left,mid-1);
        cur->right = DFS(nums,mid+1,right);
        return cur;
    }
};

 

Leetcode 108

标签:def   bst   color   int   bsp   obs   node   style   tree node   

原文地址:https://www.cnblogs.com/cunyusup/p/10338019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!