数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构
數(shù)據(jù)結(jié)構(gòu)樹二叉樹計算節(jié)點
Algorithm:
算法:
One of the popular traversal techniques to solve this kind of problems is level order tree traversal (Read: Level Order Traversal on a Binary Tree) where we use the concept of BFS.
解決此類問題的一種流行的遍歷技術(shù)是級別順序樹遍歷(閱讀: 二叉樹上的級別順序遍歷 ),我們使用BFS的概念。
The basic idea to solve the problem is:
解決問題的基本思路是:
Do a level order traversal and check whether the processed node has its left and right child both NULL.
進行級別順序遍歷,并檢查已處理節(jié)點的左,右子節(jié)點是否均為NULL 。
If the processed node has its left and right child both NULL, it’s a leaf node.
如果處理后的節(jié)點的左右子節(jié)點均為NULL,則為葉節(jié)點。
Increase leaf node count
增加葉節(jié)點數(shù)
Pseudocode:
偽代碼:
struct BT{ // tree node typeint data; //valuestruct BT *left; //pointer to left childstruct BT *right; //pointer to right child };int noofleafnodes(struct BT *root){ // root of the tree// BT refers to node of tree (datatype for the node);struct BT *temp; struct Queue *q=Creat_queue(); //creating a queueint count=0;if(!root) //root is nullreturn;//**using level oder search**EnQueue(q,root); // Enqueue the root in the queuewhile(!emptyQueue(q)){temp=DeQueue(q); //Dequeue// processing the node and checking whether both child nodes are NULL or not if(!temp->left && !temp->right) //increase no of leaves count if both child nodes are NULL (ensures it's a leaf node)count++; else{if(temp->left)EnQueue(q,temp->left); // if left child exists EnQueueif(temp->right)EnQueue(q,temp->right); // if right child exists EnQueue}}DeleteQueue(q);return count; // return no of leaf nodes }Image source: wikipedia
圖片來源:Wikipedia
Example:
例:
The leaf nodes in the above tree is 2, 5, 11, 4
上面樹中的葉節(jié)點是2、5、11、4
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}C++ implementation:
C ++實現(xiàn):
#include <bits/stdc++.h> using namespace std;class tree{ // tree node is definedpublic:int data;tree *left;tree *right; };int noofleafnodes( tree *root){queue<tree*> q; // using stltree* temp;int count=0;q.push(root);while(!q.empty()){temp=q.front();q.pop();//process node and check wheather leaf node or notif(!temp->left && !temp->right){ count++;cout<<temp->data<<" ";}if(temp->left)q.push(temp->left); //EnQueueif(temp->right)q.push(temp->right); //EnQueue}cout<<endl;return count; } tree* newnode(int data) // creating new node { tree* node = (tree*)malloc(sizeof(tree)); node->data = data; node->left = NULL; node->right = NULL; return(node); } int main() { //**same tree is builted as shown in example**int c;tree *root=newnode(2); root->left= newnode(7); root->right= newnode(5); root->right->right=newnode(9);root->right->right->left=newnode(4);root->left->left=newnode(2); root->left->right=newnode(6);root->left->right->left=newnode(5);root->left->right->right=newnode(11);cout<<"printing the leaf nodes......"<<endl; c=noofleafnodes(root);cout<<"no of leaf nodes is : "<<c<<endl; return 0; }Output
輸出量
printing the leaf nodes...... 2 5 11 4 no of leaf nodes is : 4翻譯自: https://www.includehelp.com/data-structure-tutorial/find-the-number-of-leaf-nodes-in-a-binary-tree.aspx
數(shù)據(jù)結(jié)構(gòu)樹二叉樹計算節(jié)點
總結(jié)
以上是生活随笔為你收集整理的数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#象棋程序_C ++程序确定象棋方块的
- 下一篇: Java打造一款SSH客户端,已开源!