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 ...
随机推荐
- OpenJudge Trans
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include ...
- PCRE-正则库及用法
摘自http://blog.chinaunix.net/uid-26575352-id-3517146.html 在C语言中利用PCRE实现正则表达式 http://www.pcre.org/ ...
- PHP Database ODBC 之 ODBC
ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数据库). 创建 ODBC ...
- UGUI实现的虚拟摇杆,可改变摇杆位置
实现方式主要参考这篇文章:http://www.cnblogs.com/plateFace/p/4687896.html. 主要代码如下: using UnityEngine; using Syste ...
- 【Java基础】foreach循环
从一个小程序说起: class lesson6foreach { public static void main(String[] args) { int array[]={2,3,1,5,4,6}; ...
- 【Remoting-5代码实现】
服务端 class RemotingServiceHelper { private static string m_protocolType; private static string urlStr ...
- DOM重绘对focus的影响
在处理获取焦点时一直不能获取到. 搜索了下资料是因为 当DOM的变化影响了元素的几何属性(宽和高),浏览器需要重新计算元素的几何属性,同样其他元素的几何属性和位置也会因此受到影响. 重排:浏览器会使渲 ...
- IntelliJ IDEA 14使用笔记
由eclipse到IDEA IDEA与eclipse的区别: IDEA的project对应eclipse的workspace: IDEA的module对应eclipse的project的. 所以要想在 ...
- 关于mysql的安装
上个学期学了数据库后,曾多次试图安装数据库,但由于电脑的种种的原因或者是安装的问题,始终没能安装成功,今天终于succeed. 一.下载 首先从www.mysql.com网站上下载mysql,我下载的 ...
- ckeditor 使用手册
CKEditor使用手册 在使用CKEditor过程中遇到了一些问题,现把它整理成手册,以便随时翻阅. 在页面<head>中引入ckeditor核心文件ckeditor.js <sc ...