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 ...
随机推荐
- android最新的工具DateHelper
最新的工具DateHelper 实用程序类,.的天数来获得一个给定的月份.过了几天去习惯或.周.一个月.日期等.. 代码例如以下: import android.annotation.Suppress ...
- (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- 我已提取并尝试使用启动脚本(./start navicat)来启动 Navicat Linux 版本号,但没有反应
具体的安装教程,參考这个navicat_for_mysql_10.0.11在linux下的安装,介绍的非常具体 參考这个 :我可否在 64-bit Linux 执行 Navicat? 推荐navica ...
- IMP-00013 目前只有 DBA 其他导入能力 DBA 导出的文件
--实例演示 ---system用户导出 C:\Users\ZML>exp system/zml file='D:\zml.dmp' log = 'D:\zml.log' tables = (z ...
- 谈谈CListCtrl 扩展风格设置方法-SetExtendedStyle和ModifyStyleEx 比較
谈谈CListCtrl 扩展风格设置方法 --------------------------------------SetExtendedStyle和ModifyStyleEx 比較 对于刚開始学习 ...
- WebService 用户名密码验证
原文:WebService 用户名密码验证 在项目开发的过程中,WebService是经常要用的,当调用WebService方法时,需要经过服务的验证才可以调用,一般就是用户名/密码验证,还有一个就是 ...
- chrome主页被篡改为360该溶液的导航
昨天,安装游戏后,,发现chrome该主页被篡改为360导航. 进入chrome设置更改主页,再次启动chrome或360导航,后来头发今天chrome快捷方式目标再加上一堆的属性后面360网站导航, ...
- iOS 9 新特性
这篇文章介绍了iOS9开发相关的简介,现在发布的设备都会搭载iOS9.这篇文章也列出了详细描述新特性的文章. iPad多线程增强 iOS9使用Slider Over, Split View, Pict ...
- 【C语言探索之旅】 第二部分第七课:文件读写
内容简介 1.课程大纲 2.第二部分第七课: 文件读写 3.第二部分第八课预告: 动态分配 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏 ...
- 读书笔记之SQL注入漏洞和SQL调优
原文:读书笔记之SQL注入漏洞和SQL调优 最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“' ...