Leetcode:convert_sorted_array_to_binary_search_tree
一、 称号
排序后的数组成二叉搜索树。
二、 分析
BST的中序遍历是一个sorted-array,再构造回去成一个BST,先将中间的元素作为根节点,这个节点的左右各自是左子树和右子树。如此递归地进行就可以。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &num) {
return addNode(num, 0, num.size()-1);
}
TreeNode *addNode(vector<int> &num, int start, int end){
if(start > end) return NULL; int mid = (start + end)/2;
TreeNode *root = new TreeNode(num[mid]);
root->left = addNode(num, start, mid-1);
root->right = addNode(num, mid+1, end);
return root;
}
};
版权声明:本文博主原创文章。博客,未经同意不得转载。
Leetcode:convert_sorted_array_to_binary_search_tree的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 几种流行Webservice控制框架
转会[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1 摘要 开发webservice应用程序中 ...
- ASP.NET 运行
ASP.NET 运行 对于ASP.NET开发,排在前五的话题离不开请求生命周期.像什么Cache.身份认证.Role管理.Routing映射,微软到底在请求过程中干了哪些隐秘的事,现在是时候揭晓了.抛 ...
- CSS设计指南之浮动与清除
原文:CSS设计指南之浮动与清除 浮动意思就是把元素从常规文档流中拿出来,浮动元素脱离了常规文档流之后,原来紧跟在其后的元素就会在空间允许的情况下,向上提升到与浮动元素平起平坐. 一.浮动 CSS设计 ...
- 算法---高速分拣(quick sort)
在前面的排序中所描述的算法.最快的排序算法是归并排序,但是有一个缺陷合并排序排序过程的需求O(N)额外的空间.本文介绍了高速的排序算法到位排序算法,所需的复杂性的额外空间O(1). 算法介绍:高速排序 ...
- 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)
原文:在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据) 我们都知道,在asp.net中修改 ...
- docker-gitlab(转)
Issues Docker is a relatively new project and is active being developed and tested by a thriving com ...
- 华为-on演习--身高找到最好的二人
称号: 从5个人选择2作为个人礼仪器.中的每个个体的身高的范围160-190,要求2各高度差值至少(假设差异值同样的事情,他们中最高的选择),输出的两个人的身高升序. Smple input:161 ...
- c++学籍管理系统
程序在编译时出错(vc++ 6.0) 求哪位大神帮忙改改 #include<iostream> #include <string> #include<conio.h> ...
- phpword这个问题的产生中国扭曲
采用phpword经word模板生成word当文件,乱码的问题.矿php和所有的数据库使用urf8编码格公式.解决的办法是,到场/phpword/template.php档,守则 $replace ...
- .Net程序猿乐Android发展---(10)框架布局FrameLayout
帧布局FrameLayout中全部的控件都在界面的左上側,后绘制的空间会覆盖之前的控件.布局内控件以层叠方式显示,用在游戏开发方面可能多些. 1.层叠展示 以下这个样例 ...