108. Convert Sorted Array to balanced Binary Search Tree

108. Convert Sorted Array to balanced Binary Search Tree

The tricky part is the base case .
Write induction part first and then test arrays of different size, 0, 1,2, 3
And finalize the base case 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
      if(nums.length == 0){
        return null;
      }

      TreeNode root = helper(nums, 0, nums.length - 1); // nums.length - 1
      return root;
    }

    private TreeNode helper(int[] nums, int start, int end){
      // this base case, try 4 cases. When size is 0, 1, 2 , 3
      if(start > end) return null;

      int mid = start + (end - start) / 2;
      TreeNode root = new TreeNode(nums[mid]);
      root.left = helper(nums, start, mid - 1);  // mid - 1
      root.right = helper(nums, mid + 1, end);  // mid + 1
      return root;
    }
}

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9490881.html

时间: 2024-11-08 08:14:14

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

Convert Sorted Array to Balanced Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST 解答: 1 public class Solution { 2 3 public static void main(String[] args) { 4 int[] num = {1,2,3,4,5}; 5 6 TreeNode root = sortedArrayToBST(num); 7 8

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

Convert Sorted List to Balanced Binary Search Tree (BST)

(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Code: BinaryTree* sortedListToBST(ListNode *& list, int start, int

Convert Sorted List to Balanced Binary Search Tree leetcode

题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链表的中间节点,于是链表被分为了由该中间节点划分开来的两部分.递归地处理这两部分,最终便得到了平衡二叉查找树. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *n

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

108. Convert Sorted Array to Binary Search [Python]

108. Convert Sorted Array to Binary Search 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 sub

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

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;