求二叉树叶子结点及其个数

先序遍历、中序遍历、后序遍历,其实本质都是一样的

                                   图片来源于传智播客课程

//求叶子节点个数,先序
int sum = 0;
void Ynumber(BinNode * root)
{
    if (root)
    {
        if ((root->lchild == NULL) && (root->rchild == NULL))
        {
            sum++;
            cout << "叶子" << sum << ":" << root->data << endl;
            return;
        }
        if (root->lchild)
            Ynumber(root->lchild);
        if (root->rchild)
            Ynumber(root->rchild);
    }
}
//求叶子节点个数,先序,把num作为参数传进去
void Ynumber2(BinNode * root, int * const num)
{
    if (root)
    {
        if ((root->lchild == NULL) && (root->rchild == NULL))
        {
            (*num)++;
            cout << "叶子" << sum << ":" << root->data << endl;
            return;
        }
        if (root->lchild)
            Ynumber2(root->lchild,num);
        if (root->rchild)
            Ynumber2(root->rchild,num);
    }
}
//求叶子节点个数,先序,把num作为参数传进去。num++语句段放哪里都一样,只是遍历时访问的时机不一样。
void Ynumber3(BinNode * root, int * const num)
{
    if (root)
    {
        /*
        if ((root->lchild == NULL) && (root->rchild == NULL))
        {
            (*num)++;
            cout << "叶子" << sum << ":" << root->data << endl;
            return;
        }
        */
        if (root->lchild)
            Ynumber3(root->lchild, num);

        if ((root->lchild == NULL) && (root->rchild == NULL))
        {
            (*num)++;
            cout << "叶子" << sum << ":" << root->data << endl;
            return;
        }

        if (root->rchild)
            Ynumber3(root->rchild, num);
        /*
        if ((root->lchild == NULL) && (root->rchild == NULL))
        {
            (*num)++;
            cout << "叶子" << sum << ":" << root->data << endl;
            return;
        }
        */
    }
}
int main()
{
    BinNode t1, t2, t3, t4, t5;
    memset(&t1, NULL, sizeof(t1));
    memset(&t2, NULL, sizeof(t2));
    memset(&t3, NULL, sizeof(t3));
    memset(&t4, NULL, sizeof(t4));
    memset(&t5, NULL, sizeof(t5));
    t1.data = 1;
    t2.data = 2;
    t3.data = 3;
    t4.data = 4;
    t5.data = 5;
    t1.lchild = &t2;
    t1.rchild = &t3;
    t2.lchild = &t4;
    t3.rchild = &t5;
    //遍历
    Preindex(&t1);
    cout << endl;
    Middleindex(&t1);
    cout << endl;
    Lastindex(&t1);
    cout << endl;
    //中序遍历求叶子节点个数,其他遍历相似
    Ynumber(&t1);
    cout << sum << endl;
    int num = 0;
    Ynumber2(&t1,&num);
    cout << num << endl;
    int num1 = 0;
    Ynumber3(&t1, &num1);
    cout << num1 << endl;
    return 0;
}

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。以下是一个Python实现的二叉树类,可以实现你所请的功能: ```python class Node: def __init__(self, val, pos): self.left = None self.right = None self.val = val self.pos = pos class BinaryTree: def __init__(self): self.root = None def insert(self, val, pos): if not self.root: self.root = Node(val, pos) else: self._insert(val, pos, self.root) def _insert(self, val, pos, cur_node): if val < cur_node.val: if not cur_node.left: cur_node.left = Node(val, pos) else: self._insert(val, pos, cur_node.left) elif val > cur_node.val: if not cur_node.right: cur_node.right = Node(val, pos) else: self._insert(val, pos, cur_node.right) else: raise ValueError("Duplicate value found in the tree!") def inorder_traversal(self): if not self.root: return [] else: return self._inorder_traversal(self.root) def _inorder_traversal(self, cur_node): if not cur_node: return [] else: return self._inorder_traversal(cur_node.left) + [(cur_node.val, cur_node.pos)] + self._inorder_traversal(cur_node.right) ``` 你可以用以下代码来测试这个二叉树类的功能: ```python tree = BinaryTree() tree.insert(5, (1, 2)) tree.insert(3, (2, 3)) tree.insert(7, (3, 4)) tree.insert(1, (4, 5)) tree.insert(4, (5, 6)) tree.insert(6, (6, 7)) tree.insert(8, (7, 8)) print(tree.inorder_traversal()) ``` 这个程序将会输出以下结果: ``` [(1, (4, 5)), (3, (2, 3)), (4, (5, 6)), (5, (1, 2)), (6, (6, 7)), (7, (3, 4)), (8, (7, 8))] ``` 这个结果表示二叉树中每个叶子结点的值和位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值