leetcode 108. Convert Sorted Array to Binary Search Tree
Posted hwd9654
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 108. Convert Sorted Array to Binary Search Tree相关的知识,希望对你有一定的参考价值。
其实就是一个二分搜索
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的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 108. Convert Sorted Array to Binary Search Tree
Leetcode 108. Convert Sorted Array to Binary Search Tree
LeetCode_108. Convert Sorted Array to Binary Search Tree
[LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree