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, *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */

int Depth(BiTree T);

int main()
{
	BiTree T = Create();

	printf("%d\n", Depth(T));
	return 0;
}
/* 你的代码将被嵌在这里 */

输出样例(对于图中给出的树):

4
int Depth(BiTree T){
    if(T==NULL)
        return 0;
    int a=Depth(T->lchild);
    int b=Depth(T->rchild);
    if(a>b)
        return a+1;
    else
        return b+1;
}

原文地址:https://www.cnblogs.com/DirWang/p/11930001.html

时间: 2024-08-24 18:11:03

PTA 求二叉树的深度的相关文章

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(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=

求二叉树的深度代码实现

用递归的方案实现: 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

[华为机试练习题]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

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

////计算二叉树的深度和宽度:深度:层数   宽度:各层最大节点数 ///关于二叉树问题,一般都要用到递归的方法. ////算法:首先构造二叉树节点的结构:要创建二叉树,创建的过程,使用递归算法:其次,计算二叉树深度,也要递归:最难的一点是计算求出层次中的最大节点数,使用队列的方法 #include <iostream>#include <queue>#include <tchar.h>using namespace std;struct BTNode{ char m

求二叉树的深度和广度算法

1.常见二叉树数据结构如下: /* * Definition for a binary tree node. */ struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; 2.二叉树的深度 可以使用递归算法分别求出左子树和右子树的深度,两个深度的较大值 +1 即可.代码如下: public static int getMa

求二叉树的深度 python

题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.解题思路 两种解法,一种是第一时间的思路,即获得根节点到每个叶节点的深度,取其中最长的返回: class Solution:    def __init__(self):        self.maxDeep = 0        self.curDeep = 0 def TreeDepth(self, pRoot):        if not pRoot:   

求二叉树的深度和宽度[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);