簡體   English   中英

如何在二元非排序樹中找到葉節點

[英]How to find a leaf node in a binary NOT SORTED tree

我想在未排序的二叉樹中找到一個葉節點,並能夠在其他功能中使用它。 我有這個想法

更新****

node * leaf(node* root)
{
    if(root==NULL)
        return NULL;
    if(root->left==NULL && root->right==NULL)
        return root;
    else
    {   leaf(root->left);
        leaf(root->right);
    }
}

嘗試這個:

如果root沒有子節點,則root是葉節點。

如果root擁有左子節點,則左子節點必須具有葉節點。 和合適的孩子一樣。

node* leaf(node* root) {
    if(root == NULL)
        return NULL;
    if(root->left == NULL && root->right == NULL) {
        return root;
    } else if(root->left != NULL) {
        return leaf(root->left);
    } else {
        return leaf(root->right);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM