编写求一棵二叉树中结点总数的算法。
int total_dfs(Tree& root) { if(root == NULL) return 0; return total_dfs(root->left) + total_dfs(root->right) + 1; }
int cnt = 0; int total(BitTree *root) { if(!root) return cnt; total(root -> l); total(root -> r); cnt ++; return cnt; }
int num;
int number(btree *r){
if(r){return 0 ;}
num++;
number(r->lifet);
number(r->right);
}
int NodeNum(BiTree *T){
if(!T) return 0;
else
return 1 + NodeNum(T -> lchild) + NodeNum(T -> rchild);
typedef struct TreeNode{ int val; struct TreeNode* left; struct TreeNode* right; }TreeNode; int count_node(TreeNode* root){ if(!root) return 0; return 1 + count_node(root->left, root->right); }
typedef struct TreeNode { //定义二叉树结点 int value; struct TreeNode* left; struct TreeNode* right; } TreeNode; int countNodes(TreeNode* root) { if (root == NULL) return 0; return 1 + countNodes(root->left) + countNodes(root->right); //计算结点总数 }
int computedNode(Tree *t){
if(t==NULL) return 0;
int left=computedNode(t->lchild);
int right=computedNode(t->rchild);
return 1+left+right;
11
void count_preorder(Bitree *t, int *n)
{
if(t!=NULL)
{*n++;
count_preorder(t->lchild);
count_preorder(t->lchild); }
return 1 + countNodes(t->lchild) + countNodes(t->rchild);
typedef struct BiNode{ int data; struct BiNode *lchild,*rchild; }BiNode,*BiTree; int Sum(BiNode *T){ if(T == NULL) return 0; else{ return 1 + Sum(T->lchild) + Sum(T->rchild); } }
/*求二叉树结点总数*/ int Count(Bintree T) { if(T==NULL) return 0; /*空二叉树结点数为0*/ else /*左右子树结点总数加1*/ return Count(T->lchild)+Count(T->rchild)+1; }
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
int count(BiTree T){
if(T==NULL)
return 0;
else return 1+count(T->lchild)+count(T->rchild);
int node_num(BiTree *t){ if(t==NULL) return 0; int cnt =1; cnt += node_num(t->lchild); cnt += node_num(t->rchild); return cnt ; }
void count(BiTree T){
int n=0;
if(T!=NULL){
count(T->lchild);
count(T->rchild);
n++;
return n;
加➕ 回复 q992646222_: return 值的时候,用void会报错 建议用bool
int GetNodesNum(BTNode * root)
if(root==NULL) return 0;
if(root->lchild) return GetNodesNum(root->lchild)+1;
if(root->rchild) return GetNodesNum(root->rchild)+1;
return 1;
int func(BT root)
{if(root==NULL) return 0;
int sum=1;
return sum+func(BT->lchild)+func(BT->rchild);
#include <iostream> // 二叉树节点结构 struct TreeNode { int data; TreeNode* left; TreeNode* right; TreeNode(int val) : data(val), left(nullptr), right(nullptr) {} }; // 计算二叉树节点总数的递归函数 int countNodes(TreeNode* root) { if (root == nullptr) { return 0; // 如果树为空,返回0 } // 递归计算左子树和右子树的节点总数,然后加上根节点(1) int leftCount = countNodes(root->left); int rightCount = countNodes(root->right); return leftCount + rightCount + 1; }
void count_preorder(Bitree *t, int *n){
if(t!=NULL){
*n++;
int getSize(Tree* t){
if(t==NULL){
int sum=0;
TreeNode* p=t;
if(p!=NULL){
sum=sum+getSize(p->lchild);
sum=sum+1;
sum=sum+getSize(p-rchild);
return sum;
int nodenum(Bitree t){ if(t) return 1+nodenum(t->lchild)+nodenum(t->rchild); else return 0; }
int count(BTree *p)
if(p!=NULL)
sum++;
count(p->lchild);
count(p->rchild);
int sumpoint(BTNode *p) { int n = 0; if (p != NULL) { n++; sumpoint(p->lchild); sumpoint(p->rchild); } return n; }
答案: (以先序遍历的方...
用户登录可进行刷题及查看答案
答案: (以先序遍历的方法为例)
登录后提交答案