108. Convert Sorted Array to Binary Search Tree

0人阅读 评论(0) 收藏 举报
分类:

问题描述:将一个已排好序的数组构建成BST树。
思路:一个BST树从上向下看就是一个排好序的数组。所以取中间数字为根节点,然后递归构建左右子树。
原答案:

    public TreeNode sortedArrayToBST(int[] nums) {
        int length=nums.length;
        if(length==0)
            return null;
        if(length==1)
            return new TreeNode(nums[0]);
        int pos=length/2;
        TreeNode root=new TreeNode(nums[pos]);
        root.left=sortedArrayToBST(nums,0,pos-1);
        if(length>2)
            root.right=sortedArrayToBST(nums,pos+1,length-1);
        return root;
    }

    private TreeNode sortedArrayToBST(int[] nums,int begin,int end) {
        int length=end-begin+1;
        int pos=length/2+begin;
        TreeNode root=new TreeNode(nums[pos]);
        if(pos-begin==1)
            root.left=new TreeNode(nums[begin]);
        else if(pos-begin>1)
            root.left=sortedArrayToBST(nums,begin,pos-1);
        if(end-pos==1)
            root.right=new TreeNode(nums[end]);
        else if(end-pos>1)
            root.right=sortedArrayToBST(nums,pos+1,end);
        return root;
    }

最佳答案:

 public TreeNode sortedArrayToBST(int[] nums) {
        return buildTree(nums, 0, nums.length - 1);
    }

    private TreeNode helper(int[] nums, int start, int end) {
        if (start > end) {
            System.out.println(start + ", " + end);
            return null;
        }

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

        return root;
    }

    private TreeNode buildTree(int[] num, int start, int end) {
        if (start > end) {
            return null;
        }

        TreeNode node = new TreeNode(num[(start + end) / 2]);
        node.left = buildTree(num, start, (start + end) / 2 - 1);
        node.right = buildTree(num, (start + end) / 2 + 1, end);
        return node;
    }

思路是一致的。但是边界条件判断的太复杂了。不够精简

查看评论

LeetCode(108)Convert Sorted Array to Binary Search Tree

题目如下: 分析如下: 递归进行。每次找到排序数组的中点,中点左边的子排序数组构成左子树,中点右边的子排序数组构成右子树。就这样递归进行。由于每次都几乎做到了二分,所以每次的左子树和右子树的节点数量是...
  • feliciafay
  • feliciafay
  • 2014-01-18 02:17:01
  • 2080

108. Convert Sorted Array to Binary Search Tree Leetcode Python

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 首先...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-16 07:36:06
  • 511

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

很有意思的一道题目。要求根据一个有序数组,构造出一棵高度平衡的BST。 每次找到数组的中间位置,这个便是BST的 根节点。左右孩子也很好找,根节点左边区域的中间节点便是左孩子,根节点的右边区域的中间...
  • bruce128
  • bruce128
  • 2016-02-25 19:35:34
  • 2130

leetCode 108. Convert Sorted Array to Binary Search Tree JAVA

108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascen...
  • chenchaofuck1
  • chenchaofuck1
  • 2016-04-03 14:29:49
  • 660

Convert Sorted Array to Binary Search Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/  这道题是二分查找树的题目,要把一个...
  • linhuanmars
  • linhuanmars
  • 2014-04-17 07:56:34
  • 13788

leetcode_c++:树:Convert Sorted Array to Binary Search Tree(108)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.//为...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-08-25 16:28:01
  • 208

LeetCode之路:108. Convert Sorted Array to Binary Search Tree

LeetCode之路:108. Convert Sorted Array to Binary Search Tree 一、引言真是很久很久没有刷 LeetCode 了呢!自从掉入了 Cef3 的坑之后...
  • u012814856
  • u012814856
  • 2017-09-08 15:52:38
  • 221

[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 ...
  • zdavb
  • zdavb
  • 2015-08-11 13:13:10
  • 264

leetcode_question_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. R...
  • doc_sgl
  • doc_sgl
  • 2013-09-22 23:34:23
  • 917

108. Convert Sorted Array to Binary Search Tree c++

1、题目:点击打开链接 2、给定一个数组,将其转换为一个平衡二叉查找树 Given an array where elements are sorted in ascending order, c...
  • tzh476
  • tzh476
  • 2016-09-11 02:38:30
  • 188
    个人资料
    持之以恒
    等级:
    访问量: 1985
    积分: 738
    排名: 7万+
    文章分类
    文章存档