C++求二叉树的最大高度差
#include <iostream>
#include <string.h>
using namespace std;
template<typename Type>
struct Node
{
Type data;
Node *left;
Node *right;
Node(Type d = Type()):data(d),left(NULL),right(NULL){}
//vs2013太变态了,一个空格出现未知文件尾出错,我找了10分钟。
};
template<typename Type>
class Tree
{
public:
Tree()
{
root = NULL;
flags = '#';
}
void Insert(const char *str)
{
Insert(root, str);
}
void Printf()
{
Printf(root);
}
int GetLength()//求最大高度差。
{
int i = GetLengthMax();
int j = GetLengthMin();
return i - j;
}
int GetLengthMax()
{
return GetLengthMax(root);
}
int GetLengthMin()
{
return GetLengthMin(root);
}
private:
int GetLengthMax(Node<Type> *t)
{
if (t == NULL)
return 0;
else
{
return GetLengthMax(t->left)>GetLengthMax(t->right)?GetLengthMax(t->left)+1:GetLengthMax(t->right) + 1;
}
}
int GetLengthMin(Node<Type>* t)
{
if (t == NULL)
return 0;
else
{
return GetLengthMin(t->left)<GetLengthMin(t->right) ? GetLengthMin(t->left) + 1 : GetLengthMin(t->right) + 1;
}
}
void Printf(Node<Type> *t)
{
if (t != NULL)
{
cout << t->data << " ";
Printf(t->left);
Printf(t->right);
}
}
void Insert(Node<Type> *&t, const char*& s)
{
if (*s == flags)
{
t = NULL;
return;
}
else
{
t = new Node<Type>(*s);
Insert(t->left,++s);
Insert(t->right,++s);
}
}
private:
Node<Type> *root;
char flags;
};
int main()
{
Tree<char> t;
char *s = new char[20];//1234####56##7
strcpy(s,"1##");
t.Insert(s);
t.Printf();
cout << endl;
cout << t.GetLength()<< endl;
return 0;
}
C++求二叉树的最大高度差的更多相关文章
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 求二叉树的宽度C语言版
/*层次遍历二叉树,每一层遍历完成以后都重新插入特定的指针 (比如本例使用的特殊指针是数据元素为#,左右儿子为空的指针), 这样在每次访问到所指向数据为#的队列中的结点指针是就知道该指针是这层的末尾, ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 分别求二叉树前、中、后序的第k个节点
一.求二叉树的前序遍历中的第k个节点 //求先序遍历中的第k个节点的值 ; elemType preNode(BTNode *root,int k){ if(root==NULL) return ' ...
- 求二叉树第n层节点数
在知乎看到今日头条的一个面试题“求二叉树第n层节点数”:https://zhuanlan.zhihu.com/p/25671699,想到了这样一个解法,欢迎大家交流 我的解法采用递归的思想,从0层开始 ...
- 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离
数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...
- 求二叉树第K层的节点个数+求二叉树叶子节点的个数
size_t _FindLeafSize(Node* root) //求二叉树叶子节点的个数 { //static size_t count = 0; if ...
随机推荐
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
- ArcSDE for Oracle表空间管理——暂时(TEMP)表空间
Oracle暂时表空间主要用来做查询和存放一些缓冲区数据.暂时表空间消耗的主要原因是须要对查询的中间结果进行排序. 重新启动数据库能够释放暂时表空间,假设不能重新启动实例,而一直保持问题sql语句的运 ...
- lua 类继承和实现
http://blog.csdn.net/ssihc0/article/details/7742323 Account={balance=}; --新建了一个对像,他有一个属性balance func ...
- MVC4中 jquery validate 不用submit方式验证表单或单个元素
正确引入MVC4 jquery验证的相关文件 <script src="/Scripts/jquery-1.4.4.js"></script> <sc ...
- LR实战之Discuz开源论坛——登录脚本
脚本业务流:访问Discuz论坛首页——登录论坛——退出论坛.本次使用LoadRunner11版本. 一.录制脚本注意 1.确保Discuz论坛能在服务器运行正常. 2.录制前先试访问Discuz论坛 ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
- 【Java基础】foreach循环
从一个小程序说起: class lesson6foreach { public static void main(String[] args) { int array[]={2,3,1,5,4,6}; ...
- 自学HTML的几个例子
此处不赘述HTML中不同标签的用法仅仅给出自己学习时写的一些自娱自乐的例子,具体标签用法请参考W3C(http://www.w3school.com.cn/),毕竟这个才是最靠谱的,请不要相信任何二道 ...
- 网页被Chrome识别成英语,区域,语言,网站
修改成这个后解决 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- 查看DB文件的空间使用情况
可以使用如下语句获得DB文件的空间使用 use dbName SELECT DB_NAME() AS DbName, name AS FileName, size/128.0 AS CurrentSi ...