求二叉树的深度和宽度 ----我对默认构造函数的理解

////计算二叉树的深度和宽度:深度:层数   宽度:各层最大节点数

///关于二叉树问题,一般都要用到递归的方法。

////算法:首先构造二叉树节点的结构;要创建二叉树,创建的过程,使用递归算法;其次,计算二叉树深度,也要递归;最难的一点是计算求出层次中的最大节点数,使用队列的方法

#include <iostream>
#include <queue>
#include <tchar.h>
using namespace std;
struct BTNode{
char m_value;
BTNode* m_left;
BTNode* m_right;

};

void CreateBtree(BTNode* &pRoot)
{
char nValue = 0;
cin>>nValue;
if(nValue == ‘#‘)
return;
/////注意啦!!!new BTNode()调用默认构造函数
///默认构造函数的作用是将结构体或class中的基本变量做初始化
//比如int类型初始化为0,string类型初始化"";指针类型初始化为NULL
///这一点非常重要,如果创建一个节点,其m_left和m_right别初始化为NULL
////一个节点是否为叶子节点就是通过判断其左右孩子是否为null来判断的
pRoot = new BTNode();
pRoot->m_value = nValue;
CreateBtree(pRoot->m_left);
CreateBtree(pRoot->m_right);
}

int GetDepth(BTNode* pRoot)
{
if(pRoot == NULL)
return 0;
int lLen = 0;
int rLen = 0;
lLen = GetDepth(pRoot->m_left);
rLen = GetDepth(pRoot->m_right);
return lLen > rLen ? lLen+1 : rLen+1;
}

int GetWidth(BTNode* pRoot)
{
if(pRoot == NULL)
return 0;
queue<BTNode*> que;
BTNode* pCur= NULL;
que.push(pRoot);
int iWidth= 1;
int iLastLen = 1;
int iCurLen =0;
int iTempLastLen = 0;
while(!que.empty())
{
iTempLastLen = iLastLen;
pCur = que.back();
que.pop();
while(iTempLastLen != 0)
{
if(pCur->m_left != NULL)
que.push(pCur->m_left);
if(pCur->m_right != NULL)
que.push(pCur->m_right);
iTempLastLen--;
}
iCurLen = que.size();
iWidth = iWidth > iCurLen ? iWidth : iCurLen;
iLastLen = iCurLen;
}
return iWidth;

}

int main()
{
BTNode *pRoot = NULL;
CreateBtree(pRoot);
int idepth = 0;
idepth = GetDepth(pRoot);
int iwidth = 0;
iwidth = GetWidth(pRoot);
cout <<idepth<< " "<<iwidth <<endl;
return 0;
}

时间: 2024-08-04 19:42:47

求二叉树的深度和宽度 ----我对默认构造函数的理解的相关文章

[华为机试练习题]42.求二叉树的深度和宽度

题目 题目标题: 求二叉树的宽度和深度 给定一个二叉树,获取该二叉树的宽度和深度. 例如输入 a / b c / \ / d e f g 返回3. 接口说明 原型: int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight) 输入参数: head 需要获取深度的二叉树头结点 输出参数(指针指向的内存区域保证有效): pulWidth 宽度 pulHeight 高度 返回值: 0 成功 1 失败或

[数据结构]求二叉树的深度与宽度

二叉树数据结构声明: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; 1.递归求二叉树深度 int getDepth(TreeNode *root) { if (root == NULL) { return 0; } return getDepth(root->left) > getDepth(root->right) ?(getDepth(root->left) + 1) : (getDepth(roo

求二叉树的深度和宽度[Java]

这个是常见的对二叉树的操作.总结一下: 设节点的数据结构,如下: 1 class TreeNode { 2 char val; 3 TreeNode left = null; 4 TreeNode right = null; 5 6 TreeNode(char _val) { 7 this.val = _val; 8 } 9 } 1.二叉树深度 这个可以使用递归,分别求出左子树的深度.右子树的深度,两个深度的较大值+1即可. 1 // 获取最大深度 2 public static int get

求二叉树的深度和宽度

深度: int length(BiTree t) {           int depth1 = 0;           int depth2 = 0;                      if(t == null ) return 0;           //右子树的深度           depth1 = length(t.right);           //左子树的深度           depth2 = length(t.left);                 

二叉树的深度和宽度

// 求二叉树的深度和宽度.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include <queue> using namespace std; struct BTNode { char m_value; BTNode *m_left; BTNode *m_right; }; //先序创建二叉树 void CreatBTree(BTNode *&root) { char nV

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: 1 typedef struct TreeNode{ 2 int data; 3 struct TreeNode *left; 4 struct TreeNode *right; 5 }TreeNode; 2.创建根节点: 1 TreeNode *creatRoot(){ 2 TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode)); 3 if(NULL=

soj 1034 Forest_求树的深度和宽度

题目链接 题意:给你n个节点,m条边,每条边是有向的,这颗树不能有自环,问这颗树的深度和宽度 思路: 不合法情况 1,入度大于1,即存在两条指向同一顶点的边 2,一条入点和出点都相同的边 3,一条变得入点和出点深度已知,但不符合出点的深度是入点的深度加1 4,点入深度未知但出点深度已知 5,遍历完以后,有顶点未遍历,说明有多个根 树的宽度是指,同一层最多有多少个节点 #include <iostream> #include<cstdio> #include<cstring&g

求二叉树的深度代码实现

用递归的方案实现: 1 /* 求二叉树的深度 */ 2 int getDepthBiTree(BITREENODE* T) 3 { 4 int lDepth = 0, rDepth = 0; 5 6 if(T == NULL) 7 { 8 return 0; 9 } 10 else 11 { 12 lDepth = getDepthBiTree(T->lchild); 13 rDepth = getDepthBiTree(T->rchild); 14 } 15 16 return lDepth

PTA 求二叉树的深度

6-7 求二叉树的深度 (6 分) 本题要求实现一个函数,可返回二叉树的深度. 函数接口定义: int Depth(BiTree T); T是二叉树树根指针,函数Depth返回二叉树的深度,若树为空,返回0. 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> typedef char ElemType; typedef struct BiTNode { ElemType data; struct BiTNode *lchild, *r