[英]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.