标签:构建 vat return img tree alt 搜索 inf obs
其实就是一个二分搜索
mid为当前root,
然后分别用左右两个部分完成左右子树的构建
class Solution { public TreeNode sortedArrayToBST(int[] nums) { TreeNode root = helper(nums, 0, nums.length-1); return root; } private TreeNode helper (int[] nums, int left, int right){ if(left > right) return null; int mid = left + (right-left) / 2; TreeNode cur = new TreeNode(nums[mid]); cur.left = helper(nums, left, mid-1); cur.right = helper(nums, mid+1, right); return cur; } }
leetcode 108. Convert Sorted Array to Binary Search Tree
标签:构建 vat return img tree alt 搜索 inf obs
原文地址:https://www.cnblogs.com/hwd9654/p/11361507.html