判断一棵二叉树是否是平衡树 看到二叉树的题目,基本都可以用递归的思想求解。对于判断是否是一棵平衡树可分为以下几个步骤: 计算左子树和右子树的高度差是否大于1,是则返回false 判断左子树是否是平衡树,判断右子树是否是平衡树 class Solution {public: bool isBalanc...
分类:
其他好文 时间:
2015-04-09 15:18:20
阅读次数:
100
题目:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.分析:
将已经排序好的数组转成高度平衡的二叉排序树。
依旧二分法。C++参考代码:/**
* Definition for binary tree
* struct TreeNode {...
分类:
其他好文 时间:
2015-04-09 13:51:49
阅读次数:
112
Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root...
分类:
编程语言 时间:
2015-04-08 07:55:25
阅读次数:
137
G - Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3264
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000...
分类:
其他好文 时间:
2015-04-07 10:05:56
阅读次数:
139
题目地址:POJ 3264
为了学LCA在线算法,先学一下RMQ。。。RMQ第一发,纯模板题。不多说。
代码如下:#include
#include
#include
#include
#include
#include
#include
#include <s...
分类:
其他好文 时间:
2015-04-05 20:29:12
阅读次数:
143
判断一个二叉树是否为平衡二叉树
int Depth(BinTree* root)
{
if(root == NULL)
return 0;
return max(Depth(root->left),Depth(root->right))+1;
}
bool isBalancedBinTree(BinTree* root)
{
if(root ==NULL)
return 1;
i...
分类:
其他好文 时间:
2015-04-05 12:02:53
阅读次数:
104
Convert Sorted List to Binary Search TreeGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST....
分类:
其他好文 时间:
2015-04-04 15:01:48
阅读次数:
117
题目:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
思路:从一个数组中找到中间的元素作为BST的根,然后坐边的作为左子树,右边的作为右子树,递归调用
#include
#include
#include
#include
...
分类:
其他好文 时间:
2015-04-04 12:18:29
阅读次数:
132
题目:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
思路:和上面的思想一样,只不过注意找到链表的中间节点的方法
void Inorder(BinTree* root)
{
if(root == NULL)
...
分类:
其他好文 时间:
2015-04-04 12:17:29
阅读次数:
136
思路:
走了好多弯路,最后才发现直接根据定义来就可以了,左右子树的深度不超过1且左右子树都是平衡二叉树,就是这么简洁明快。...
分类:
其他好文 时间:
2015-04-03 17:32:35
阅读次数:
128