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 ...
随机推荐
- ubuntu快捷键设置,查看系统
设置system setting于.点击键盘keyboard,有捷径keyboard shortcut.但也设置快捷键本身. 版权声明:本文博主原创文章,博客,未经同意不得转载.
- Effective C++笔记05:实现
条款26:尽可能延后变量定义式的出现时间 博客地址:http://blog.csdn.net/cv_ronny 转载请注明出处! 有些对象,你可能过早的定义它,而在代码运行的过程中发生了导常,造成了開 ...
- VS2015 C#6.0
VS2015 C#6.0 中的那些新特性 VS2015在自己机器上确实是装好了,(全部安装的话,在Java SE 开发工具包 会卡顿很长时间,我直接关闭,然后重启电脑,重新修复安装搞定), 想来体验一 ...
- JVM内存结构、垃圾回收那点事(转)
翻看电脑的文件夹,无意看到了9月份在公司做的一次分享,浏览了一下"婆婆特",发现自己在ppt上的写的引导性问题自己也不能确切的回答出来,哎,知识这东西,平时不常用的没些日子就生疏了 ...
- DuiVision开发教程(15)-DUI文本控制基础类
CControlBaseFont类是DuiVision支持所有基类的控件的文本属性. 此控件例如属性列表,下面: 物业名称 类型 说明 title 字符串 控件的显示标题 font 字体 控件的字体, ...
- Android剪裁图片简单的方法
/** * 按正方形裁切图片 */ public static Bitmap ImageCrop(Bitmap bitmap) { int w = bitmap.getWidth(); // 得到图片 ...
- JavaScript变量作用域和内存问题(二)
执行环境是js中特别重要的概念,是指变量或者函数可以访问其他数据,定义自己的行为.每个执行环境都有一个与之相对应的变量对象,执行环境中定义的所有变量和函数都保存在这个变量中,我们看不到这个变量,但是后 ...
- JAVA 跑马灯文字效果
JAVA跑马灯文字效果的实现: 1. 首先创建一个继承JFrame类的HorseRaceLightTextFrame窗体类,代码如下: package com.example.horseracelig ...
- Android组件系列----ContentProvider内容提供商【5】
2.执行query()方法,查询全部记录(眼下一共两条记录).后台输出效果例如以下: 经測试,其它方法也都是能够运行的. 事实证明,新建的另外一个project文件ContentResolverTes ...
- Windows Phone 8 ControlTiltEffect
/* Copyright (c) 2010 Microsoft Corporation. All rights reserved. Use of this sample source code is ...