【easy】108. Convert Sorted Array to Binary Search Tree


Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

//思路还是很清晰的:就是要取中间节点作为根节点,其他的分别是左子树和右子树,递归
/**
 * 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) {
        TreeNode *node = createBST(nums,0,nums.size()-1);
        return node;
    }
public:
    TreeNode* createBST(vector<int>nums, int left, int right){
        if (left>right)
            return NULL;//??
        int mid = (left+right)/2;
        TreeNode *node = new TreeNode(nums[mid]);
        node->left = createBST(nums, left, mid - 1);
        node->right = createBST(nums, mid+1, right);
        return node;
    }
};

递归出口NULL注意,最终只是要返回根节点。

原文地址:https://www.cnblogs.com/sherry-yang/p/8407397.html

时间: 2024-08-27 21:53:18

【easy】108. Convert Sorted Array to Binary Search Tree的相关文章

【leetcode】108. Convert Sorted Array to Binary Search Tree

题目如下: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by m

【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 题目大意:给定一个排好序的数组,将它转变成一个平衡二叉搜索数 解题思路:平衡二叉搜索树的中序遍历为升序数组

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh

LC.108.Convert Sorted Array to Binary Search Tree

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a bin

【LeetCode】109&amp; - Convert Sorted List to Binary Search Tree&amp;Convert Sorted Array to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Solution 1:  recursion runtime: 28ms. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i

[leedcode 108] Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ p

108.Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it toa height balanced BST. HideTags Tree  Depth-first Search #pragma once #include<iostream> #include<vector> using namespace std; struct TreeNode { int val; TreeNode *left;