日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构

發布時間:2025/3/11 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構樹二叉樹計算節點

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.

解決此類問題的一種流行的遍歷技術是級別順序樹遍歷(閱讀: 二叉樹上的級別順序遍歷 ),我們使用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.

    進行級別順序遍歷,并檢查已處理節點的左,右子節點是否均為NULL 。

  • If the processed node has its left and right child both NULL, it’s a leaf node.

    如果處理后的節點的左右子節點均為NULL,則為葉節點。

  • Increase leaf node count

    增加葉節點數

  • 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

    上面樹中的葉節點是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 ++實現:

    #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

    數據結構樹二叉樹計算節點

    總結

    以上是生活随笔為你收集整理的数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。