设为首页 加入收藏

TOP

求二叉树节点数,递归与非递归
2015-02-02 14:15:37 来源: 作者: 【 】 浏览:8
Tags:点数

1、二叉树定义


typedef struct BTreeNodeElement_t_ {
? ? void *data;
} BTreeNodeElement_t;


typedef struct BTreeNode_t_ {
? ? BTreeNodeElement_t *m_pElemt;
? ? struct BTreeNode_t_? ? *m_pLeft;
? ? struct BTreeNode_t_? ? *m_pRight;
} BTreeNode_t;


2、求二叉树节点数


对任意一个给定子树的节点数,左子树节点数+右子树节点数+1,加1是加上根节点自身


(1)递归方式:


如果给定根节点为NULL,则返回0;


如果给定根节点不为NULL,则返回: 左子树节点数+右子树节点数+1


int GetBTreeNodesTotal( BTreeNode_t *pRoot){
? ? if( pRoot == NULL )
? ? ? ? return 0;


? ? return ( GetBTreeNodesTotal( pRoot->m_pLeft) + GetBTreeNodesTotal( pRoot->m_pRight) + 1);
}


(2)非递归方式:


任意一种遍历方式: 前序、后序、中序、按层遍历方式都可以


前序、后序、中序、按层遍历。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇求二叉树深度,递归和非递归 下一篇按层遍历二叉树

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: