开发者社区 问答 正文

非递归算法求二叉树叶子节点数

非递归算法求二叉树叶子节点数

展开
收起
知与谁同 2018-07-22 11:41:55 2082 分享 版权
1 条回答
写回答
取消 提交回答
  • int count(struct Node *root)
    {
    int n=0;
    stack s;
    if(root == null) return 0;
    s.push(root);
    while(!s.empty()){
    p = s.pop();
    if(p->left == null && p->right==null){
    n++;
    }
    if(p->left != null){
    s.push(p->left);
    }
    if(p->right != null){
    s.push(p->right);

    }
    return n;

    }
    2019-07-17 22:55:49
    赞同 展开评论
问答分类:
问答标签:
问答地址: