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 ...
随机推荐
- Path Sum 解答
Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- The Java™ Tutorials下载地址
1.The Java™ Tutorials下载地址: http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-200 ...
- Elasticsearch 安装与集群配置
一.软件版本 操作系统:CentOS-6.5-x86_64 ES版本:5.0 主机:192.168.63.246 主机: 192.168.63.242 二.部署环境规划: 1. 需求:jdk版本: ...
- 《Qt编程的艺术》——5.1 手动布局
在传统的GUI设计中,每个控件(Widget)都要手动地绑定在窗口之上的一个点上(也就是说,这个控件被指定成了给定GUI元素的父对象),同时还要指定这个控件的高度和宽度.作为所有图形元素的基础类,QW ...
- 具体解释EBS接口开发之物料导入API
create_item inv_item_grp.create_item(p_commit => fnd_api.g_true, -- p_item_rec => l_item_rec, ...
- .Net 中DataSet导出为excel的方法
依旧是留下代码防止以后忘记 protected void Export_Click(object sender, EventArgs e) { DataSet data = "" ...
- Kill命令模拟1
#include<sys/types.h> #include<signal.h> #include<stdio.h> #include<stdlib.h> ...
- c++ 资源索引
1.http://snippets.dzone.com/tag/c/ --数以千计的有用的C语言源代码片段 2.http://www.hotscripts.com/category/c-cpp/sc ...
- Fragment 之 PagerAdapter
package com.edaixi.main.adapter; import android.content.Context; import android.support.v4.view.Page ...
- ORA-04092: COMMIT 不能在触发器中
触发器无需commit也不能写commit触发器和触发它的DML是同一个事务DML提交了,触发器的操作也提交了,要不就一起回滚了 当然,如果你一定要在触发器里写COMMIT那就用自治事务相当于一个事务 ...