二叉树深度和高度

In this tutorial, we will learn how to find height and depth of binary tree with program implementation in C++. It is one of the most commonly used non-linear data structures. We will learn about:

在本教程中,我们将学习如何使用C ++中的程序实现来查找二叉树的高度和深度。 它是最常用的非线性数据结构之一。 我们将了解:

  • What is the height of a binary tree?二叉树的高度是多少?
  • Algorithm and implementation for finding height of Binary tree查找二叉树高度的算法和实现
  • What is the depth of a binary tree?二叉树的深度是多少?
  • Algorithm and implementation for finding depth of Binary tree查找二叉树深度的算法和实现

Many times, people are confused between Depth and Height of Binary tree. It is because the depth of binary tree is always equal to the height of binary tree but they are not the same and using the terms interchangeably is not correct. So, it is important for us to understand the difference between the Height and Depth of Binary tree.

很多时候,人们对二叉树的深度和高度感到困惑。 这是因为二叉树的深度始终等于二叉树的高度,但是它们不相同,并且互换使用这些术语是不正确的。 因此,重要的是要了解二叉树的高度和深度之间的差异。

二叉树的高度 (Height of Binary Tree)

“Dream as high as the sky and as Deep as the ocean.”

“梦象天空一样深,象海洋一样深。”

As the quote on top says sky is what we should see while calculating height. The height of binary tree is the measure of length of the tree in the vertical direction. It is measured in upward direction that is from child to parent. The leaf nodes have height of 0 as there is no nodes below them. The height of the root node of the binary tree is the height of the whole tree. The height of a particular node is the number of edges on the longest path from that node to a leaf node.

正如最上面的报价所说,天空是计算高度时应该看到的。 二叉树的高度是垂直方向上树的长度的量度。 它是从孩子到父母的向上方向测量的。 叶节点的高度为0,因为它们下面没有节点。 二叉树的根节点的高度是整个树的高度。 特定节点的高度是从该节点到叶节点的最长路径上的边数。

Finding the Height of Binary Tree

求二叉树的高度

To find the height of the binary tree we will recursively calculate the height of the left and right subtree of a node. To find the heights of left and right subtrees we use in-order traversal. After finding the height of both left and right subtree we will store the height of the subtree which has maximum value and add 1 to it to include the current level of tree.

为了找到二叉树的高度,我们将递归计算节点左右子树的高度。 为了找到左和右子树的高度,我们使用有序遍历。 找到左右子树的高度后,我们将存储具有最大值的子树的高度,并向其添加1以包括当前树的级别。

算法 (Algorithm)

FindHeight( Node root)If root == NULL return 0elseint leftH = FindHeight ( root->left )int rightH = FindHeight(root->right ) return max( leftH, rightH )+1

用C ++程序查找二叉树的高度 (C++ Program to Find Height of Binary Tree)

#include <bits/stdc++.h> using namespace std; /* structure of a binary tree */
struct node
{  int data;    // to store the value of a node in treenode* left;   // pointer to the left child node* right;   // pointer to the right child
}; /* function to find the maximum height of binary tree */
int FindHeight(node* node)
{ if (node == NULL)  // when the subtree is emptyreturn 0; else{ int leftH,rightH;/*find the height of left subtree */leftH = FindHeight(node->left); /*find the height of right subtree */rightH = FindHeight(node->right); /* return the maximum height */if (leftH > rightH) return(leftH + 1); else return(rightH + 1); }
} /* function to get new node for the tree */
node* getNode(int data)
{ node* newNode = new node(); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return(newNode);
} int main()
{ /* creating the binary tree */node *root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->right->left = getNode(6); root->right->right = getNode(7); cout << "The Height of the binary tree is " << FindHeight(root); return 0;
}

Output:

输出:

The Height of the binary tree is 3

二叉树的高度为3

二叉树的深度 (Depth of Binary Tree)

Think of ocean and the quote above while calculating depth. The depth is a measure of how far a node is from the root of the tree. The depth of the ocean is calculated with respect to the sea level similarly the depth of any node in binary tree is measured with respect to the root node of the tree. The depth of a particular node in binary tree is the number of edges from the root node to that node. The depth of binary tree is the depth of the deepest node (leaf node).

在计算深度时,请考虑一下海洋和上面的报价。 深度是节点距树根的距离的量度。 相对于海平面计算海洋深度,类似地,相对于树的根节点,测量二叉树中任何节点的深度。 二叉树中特定节点的深度是从根节点到该节点的边数。 二叉树的深度是最深节点(叶节点)的深度。

To find the depth of the binary tree we will recursively calculate the depth of the left and right child of a node. After finding the depth of both left and right child we will store the depth of the child which has maximum value and add 1 to it to include the current level of tree.

为了找到二叉树的深度,我们将递归计算节点左右子节点的深度。 找到左右孩子的深度后,我们将存储具有最大值的孩子的深度,并向其添加1以包括当前树的级别。

算法 (Algorithm)

FindDepth( Node root)If root == NULL return 0elseint leftD = FindDepth ( root->left )int rightD = FindDepth (root->right ) return max( leftD, rightD)+1

查找二进制树深度的C ++程序 (C++ Program to Find Depth of Binary Tree)

#include <bits/stdc++.h>
using namespace std; /* structure of a binary tree */
struct node
{  int data;    // to store the value of a node in treenode* left;   // pointer to the left child node* right;   // pointer to the right child
}; /* Finding the depth of tree */
int FindDepth(node* node)
{ if (node == NULL)  // when the subtree is emptyreturn 0; else{ int leftD,rightD;/*find the depth of left child */leftD = FindDepth(node->left); /*find the depth of right child */rightD = FindDepth(node->right); /* return the maximum depth */if (leftD > rightD) return(leftD + 1); else return(rightD + 1); }
} /* function to get new node for the tree */
node* getNode(int data)
{ node* newNode = new node(); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return(newNode);
} int main()
{ /* creating the binary tree */node *root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->right->left = getNode(6); root->right->right = getNode(7); root->left->right->right = getNode(10); cout << "The Depth of the binary tree is " << FindDepth(root)-1; return 0;
}

Output:

输出:

The Depth of the binary tree is 3

二叉树的深度为3

Comment down below if you have queries related to height and depth of binary tree.

如果您有关于二叉树的高度和深度的查询,请在下面注释。

翻译自: https://www.thecrazyprogrammer.com/2019/11/height-and-depth-of-binary-tree.html

二叉树深度和高度

二叉树深度和高度_二叉树的高度和深度相关推荐

  1. java二叉树求权值_二叉树中的权值是什么?

    展开全部 二叉树中的权值就是对叶子结点赋予的一个有意义的数量值. 一棵深度为k,且有2^k-1个节点的二叉树,32313133353236313431303231363533e58685e5aeb93 ...

  2. 二叉树线索化示意图_二叉树的线索化

    二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历.二叉树作为存储结构时,一个节点只能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继.为了保存这种在 ...

  3. Java二叉树逆序遍历_二叉树遍历小结

    二叉树遍历小结 声明 0 二叉树遍历概述 二叉树遍历:按照既定序,对每个节点仅访问一次: 二叉树非递归遍历思想:参考这篇博文,核心思想是存在重合元素的局部有序保证整体有序,由于二叉树的结构特点,二叉树 ...

  4. java二叉树的深度优先遍历_二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式...

    1 packageSolution;2 3 importjava.util.LinkedList;4 importjava.util.Queue;5 importjava.util.Stack;6 7 ...

  5. element里面popover里面的高度_五斗柜的高度一般是多少 五斗柜放在什么位置好

    五斗柜也就是一种抽屉收纳柜,目前在卧室或是书房等空间均是可以见到.其根据使用用途的不同,进而有着高度和款式,以及摆放位置等等的区别.因此,下面带来五斗柜的高度一般是多少.五斗柜放在什么位置好,以及五斗 ...

  6. 二叉树后序遍历_二叉树后序遍历非递归实现

    二叉树的后序遍历非递归实现是三种遍历实现里面最复杂的一种了. 后序遍历的顺序是左节点-右节点-根节点,因为二叉树每个节点只有指向子节点的指针而没有指向父节点的指针,因此我们需要一个额外的变量来记录是否 ...

  7. 二叉树线索化示意图_二叉树的线索化算法思想详解

    二叉树的线索化,这几天以来我很难掌握,今天终于想通了,哈哈,首先我们来看看二叉树线索化之后会变成什么样子,这里我们以图中的二叉树为例,图如下: 画的太糙,各位看官讲究着看吧- -.所谓二叉树的线索化, ...

  8. Java交换二叉树的左右子树_二叉树左右子树交换

    1. 递归 2. 栈 package org.skyeye.test; import java.util.Stack; public class TreeSwap { public static cl ...

  9. 二叉树线索化示意图_二叉树线索化

    程序代码: 1 #include 2 using namespacestd;3 typedef enum 4 {5 LINK,6 THREAD7 }PointerTag;8 template 9 st ...

最新文章

  1. 程序员的生活就是这么朴实无华,且没钱
  2. Zabbix,Nagios,OneAPM Servers 安装部署
  3. STM32中FLASH_Status的5个状态各表示的含义详解
  4. Java堆和栈的区别
  5. 453. 最小操作次数使数组元素相等
  6. 《TCP/IP详解》学习笔记(一):基本概念
  7. scala python_Scala与Python | 哪种编程语言更好
  8. 找出不是两个数组共有的元素
  9. 什么叫云平台_为什么说云原生会成为未来企业技术变迁的趋势
  10. 推荐系统 | 引用量超过1000的52篇经典论文
  11. 音视频开发必备基本基础知识(1)
  12. xampp安装后无法启动apache
  13. classdefnotfound本地不报错_四种解决Nginx出现403 forbidden 报错的方法
  14. android studio 新建的项目无法绘制界面
  15. web开发中多线程下载文件
  16. Win10升级后,文件夹背景变成黑色
  17. 【牛客网】滴滴出行2017秋招测试岗笔试真题汇总
  18. macOS Big Sur 11.5 (20G71) 正式版 DMG、ISO、IPSW 百度网盘下载
  19. Adjust接入注意事项
  20. python输入一个浮点数、输出其整数部分和小数部分_输入一个浮点数,并输出该数的整数部分和小数部分...

热门文章

  1. 如何挑选优质老域名?
  2. oracle里的to_nchar,浅谈Oracle中的CHAR与NCHAR数据类型 .
  3. ResNet与ResNeXt
  4. 企业机器学习可视化管理平台在信息 化项目管理中的实践研究——以某政务数据治理项目为例-4
  5. 【win10】LaTex安装踩雷记录
  6. PHP 时间操作(获取一周前、明天的日期)
  7. ossec 最全最新安装搭建ossec-2.8.3部署centos7服务器
  8. ZGC、G1与Parallel Scavenge吞吐量及停顿时间对比
  9. 【调剂】济南大学机器学习及其应用课题组拟接收计算机硕士(第一志愿及调剂)报考-预宣传...
  10. 指数高通滤波器代码_影像组学学习笔记1-2——高通滤波法及应用