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 binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

0     / \   -3   9   /   / -10  5

time: o(n) space: o(height)pre order: 先弄自己, 再管下面的兄弟
public TreeNode sortedArrayToBST(int[] nums) {
        if (nums  == null || nums.length ==0 )return null ;
        int left = 0, right = nums.length -1 ;
        return helper(nums, left, right);
    }

    //everytime create the root and connect to left and right
    private TreeNode helper(int[] nums, int left, int right) {
        int mid = left + (right - left) /2 ;
        TreeNode node = new TreeNode(nums[mid]) ;
        if (mid+ 1<=right){
            node.right = helper(nums, mid+1, right) ;
        }
        if (mid -1 >= left){
            node.left = helper(nums, left, mid-1) ;
        }
        //going down and repeat
        return node ;
    }


原文地址:https://www.cnblogs.com/davidnyc/p/8495669.html

时间: 2024-12-26 23:57:43

LC.108.Convert Sorted Array to Binary Search Tree的相关文章

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

【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

[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;

leetcode 108 Convert Sorted Array to Binary Search Tree ----- java

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; * TreeNo

Java for 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. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return

【一天一道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. (二)解题 题目大意:给定一个排好序的数组,将它转变成一个平衡二叉搜索数 解题思路:平衡二叉搜索树的中序遍历为升序数组