美文网首页
108. Convert Sorted Array to Bin

108. Convert Sorted Array to Bin

作者: 番茄晓蛋 | 来源:发表于2017-09-18 09:22 被阅读26次

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
解题思路: dfs ,按照bst的定义,从nums的中点选做head, 然后左右两边去dfs

 public TreeNode sortedArrayToBST(int[] nums) {
      if (nums.length == 0) {
          return null;
      }
      TreeNode head = helper(nums, 0, nums.length - 1);
      return head;
    }
    public TreeNode helper(int[] nums, int low, int high) {
        if (low > high) { // Done
            return null;
        }
        int mid = (low + high) /2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = helper(nums, low, mid - 1);
        node.right = helper(nums, mid + 1, high);
        return node;
    }

相关文章

网友评论

      本文标题:108. Convert Sorted Array to Bin

      本文链接:https://www.haomeiwen.com/subject/bwuvsxtx.html