Convert Sorted Array to Binary Search Tree With Minimal Height
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height.
Given [1,2,3,4,5,6,7]
, return
4
/ \
2 6
/ \ / \
1 3 5 7
分析:
这是一道非常明显的递归题。取array的中间数作为树的root,array 左边部分是左子树部分,array右边部分是右子树部分。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param A: an integer array
* @return: a tree node
* cnblogs.com/beiyeqingteng/
*/
public TreeNode sortedArrayToBST(int[] A) {
if (A == null || A.length == ) return null;
return helper(A, , A.length - );
} public TreeNode helper(int[] A, int i, int j) {
if (i == j) {
return new TreeNode(A[i]);
} else if (i > j) {
return null;
} else {
int mid = i + (j - i) / ;
TreeNode root = new TreeNode(A[mid]);
root.left = helper(A, i, mid - );
root.right = helper(A, mid + , j);
return root;
}
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
Convert Sorted Array to Binary Search Tree With Minimal Height的更多相关文章
- 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】
Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...
- 【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height
题目: Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. ...
- LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy
177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order ...
- [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
随机推荐
- 【HDU 5105】Math Problem
题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间 ...
- 【ZOJ 1221】Risk
题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...
- 【POJ 1061】青蛙的约会
题 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...
- NOI题库-小学奥赛QwQ
今天Loli教育我们让我们来看看NOI题库的奥赛部分,不过,为何是小学的( ⊙ o ⊙ )啊!感觉智商被各种侮辱. 余数相同问题: 描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为 ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- Eclipse启动Tomcat错误:Several ports (8005,8009) required by Tomcat v6.0 Server at localhost are already
解决办法: 1.netstat -aon|findstr 8005 可查看指定端口号使用情况 2.tasklist |findstr 10452 找出占用指定进程Id的程序 3.taskkill /p ...
- linux中防CC攻击两种实现方法(转)
CC攻击就是说攻击者利用服务器或代理服务器指向被攻击的主机,然后模仿DDOS,和伪装方法网站,这种CC主要是用来攻击页面的,导致系统性能用完而主机挂掉了,下面我们来看linux中防CC攻击方法. 什么 ...
- CATransform3D
本章介绍图层的几何组成部分,及他们之间的相互关,同时介绍如何变换矩阵可以产生复杂的视觉效果. 1.1 图层的坐标系 图层的坐标系在不同平台上面具有差异性.在iOS系统中,默认的坐标系统原点在图层的中心 ...
- easyUI框架之学习2--添加左侧导航栏
<head> function addTab(title, url) { if ($('#tableContainer').tabs('exists', title)) { $('#tab ...
- boost构造,解析json
void asynDBCenter::isGetActorInfoEx(void* on_process, const char* arg) { std::stringstream ros(arg); ...