首页 > 编程知识 正文

二叉树的深度怎么看,求二叉树的最大深度

时间:2023-05-03 16:13:10 阅读:160299 作者:3793

二叉树的深度和高度

In this tutorial,wewilllearnhowtofindheightanddepthofbinarytreewithprogramimplementationinc.itisoneofthemostcommonlyusednon

在本教程中,您将学习如何使用C的程序实现来找到二叉树的高度和深度。 是最常见的非线性数据结构之一。 理解以下内容。

What is the height of a binary tree? 二叉树有多高? algorithmandimplementationforfindingheightofbinarytree

实现二叉树高度查找算法和What is the depth of a binary tree?

二叉树有多深? algorithmandimplementationforfindingdepthofbinarytree

查找二叉树深度的算法与实现

Many times, peopleareconfusedbetweendepthandheightofbinarytree.itisbecausethedepthofbinarytreeeisalwaysequaltotheheightofbinarytreeebutt het termsinterchangeablyisnotcorrect.so,itisimportantforustounderstandthedifferencebetweentheheight

很多时候,人们对二叉树的深度和高度感到困惑。 这是因为二叉树的深度总是与二叉树的高度相同,但它们是不同的,而且交换使用这些术语是不正确的。 因此,了解二叉树的高度和深度的差异很重要。

二叉树高度(Height of Binary Tree )“dreamashighastheskyandasdeepastheocean .”

梦像天空一样深,像大海一样深。 ”

asthequoteontopsaysskyiswhatweshouldseewhilecalculatingheight.theheightofbinarytreeistheasureoflengthofthetreeeeinthevert icart pwarddirectionthatisfromchildtoparent.theleafnodeshaveheightof0asthereisnonodesbelowthem.th hem eightoftherotnodeofthebinared ee.theheightofaparticularnodeisthenumberofedgesonthelongestpathfromthethenumberofedgesonthelongestpathfromthe

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

Finding the Height of Binary Tree

求二叉树的高度

tofindtheheightofthebinarytreewewillrecursivelycalculatetheheightoftheleftandrightsubtreeofanode.tofindtheheheightsofleftand aversal.afterfindingtheheightofbothleftandrightsubtreewewillstoretheheightofthesubtreewhichas ximumvalueandadd1toittoincluded

el 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五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。