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 are sorted in ascending order, convert it to a height balanced BST.

Subscribe to see which companies asked this question

Show Tags

Show Similar Problems

Have you met this question in a real interview?

Yes

No

Discuss

很有意思的一道题目。要求根据一个有序数组,构造出一棵高度平衡的BST。

每次找到数组的中间位置,这个便是BST的 根节点。左右孩子也很好找,根节点左边区域的中间节点便是左孩子,根节点的右边区域的中间节点便是右孩子。如此递归求解。

我的AC代码

public class ConvertSortedArraytoBinarySearchTree {

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

	TreeNode dfs(int[] nums, int left, int right) {
		if (left > right) return null;
		int mid = (left + right) / 2;
		TreeNode root = new TreeNode(nums[mid]);
		root.left = dfs(nums, left, mid - 1);
		root.right = dfs(nums, mid + 1, right);
		return root;
	}
}
时间: 2024-11-10 07:07:32

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解的相关文章

LeetCode OJ 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. 把一个有序的数组转换为一颗平衡二叉搜索树.每次找到中间节点作为根节点,根节点左边部分转换为左子树,右边部分转换为右子树.代码如下: 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val;

【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. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by m

【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 题目大意:给定一个排好序的数组,将它转变成一个平衡二叉搜索数 解题思路:平衡二叉搜索树的中序遍历为升序数组

LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一个排序好的数组转换成二叉搜索树,这题没想出来,基本上是参考别人的,边界条件应该注意一下: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * T

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

LC.108.Convert Sorted Array to Binary Search Tree

https://leetcode.com/problems/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. For this problem, a height-balanced binary tree is defined as a bin

Java for 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. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return

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 BST. 给一个排好序的数组,然后求搜索二叉树 其实就是二分法,不难. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNo