如何统计二叉树叶子节点数量并计算其高度?
- 内容介绍
- 文章标签
- 相关推荐
本文共计470个文字,预计阅读时间需要2分钟。
已知二叉树以二叉链表形式存储,其中节点的数据域为data。编写算法,统计二叉树中叶子节点值等于x的节点数。
ctypedef struct BTNode { int data; struct BTNode *lchild;} BTNode;
int countLeafValueEqualX(BTNode *root, int x) { if (root==NULL) return 0; if (root->lchild==NULL && root->rchild==NULL && root->data==x) { return 1; } return countLeafValueEqualX(root->lchild, x) + countLeafValueEqualX(root->rchild, x);}
1、已知二叉树以二叉链表进行存储,其中结点的数据域为data,编写算法,统计二叉树中叶子结点值等于x的结点数目。
本文共计470个文字,预计阅读时间需要2分钟。
已知二叉树以二叉链表形式存储,其中节点的数据域为data。编写算法,统计二叉树中叶子节点值等于x的节点数。
ctypedef struct BTNode { int data; struct BTNode *lchild;} BTNode;
int countLeafValueEqualX(BTNode *root, int x) { if (root==NULL) return 0; if (root->lchild==NULL && root->rchild==NULL && root->data==x) { return 1; } return countLeafValueEqualX(root->lchild, x) + countLeafValueEqualX(root->rchild, x);}
1、已知二叉树以二叉链表进行存储,其中结点的数据域为data,编写算法,统计二叉树中叶子结点值等于x的结点数目。

