C++算法之 求二叉树第k层的节点的个树

思路:

如果树为空或者k< 1,那么节点个数为0;

如果k=1,那么节点个数为1;

如果k>1,那么第k层 总节点的个数等于 左子树k-1层的节点个数+右子树k-1层节点的个数+1

代码如下:

// BTNumOfKLevel.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class BTree
{
public:
	int     m_nValue;
	BTree*  m_pLeft;
	BTree*  m_pRight;

	BTree(int m):m_nValue(m)
	{
		m_pLeft = m_pRight = NULL;
	}

};
//二叉树的插入实现
void Insert(int value, BTree* &root)
{
	if (root == NULL)
	{
		root = new BTree(value);
	}
	else if(value < root->m_nValue)
		Insert(value,root->m_pLeft);
	else if(value > root->m_nValue)
		Insert(value,root->m_pRight);
	else
		;
}

int GetNumOfKthLevel(BTree* root, int k)
{
	if(root == NULL || k < 1)
		return 0;
	if(k == 1)
		return 1;
	int LeftNum = GetNumOfKthLevel(root->m_pLeft,k-1);
	int RightNum = GetNumOfKthLevel(root->m_pRight,k-1);

	int ret = LeftNum+RightNum;
	return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
	BTree* m_pRoot = new BTree(4);
	Insert(3,m_pRoot);
	Insert(6,m_pRoot);
	Insert(1,m_pRoot);
	Insert(2,m_pRoot);
	Insert(5,m_pRoot);
	Insert(8,m_pRoot);
	Insert(7,m_pRoot);
	Insert(10,m_pRoot);

	int number = GetNumOfKthLevel(m_pRoot,4);
	cout<<"第4层上的节点的个数为:"<<number<<endl;

	getchar();
	return 0;
}
时间: 2024-08-26 12:07:36

C++算法之 求二叉树第k层的节点的个树的相关文章

求二叉树第K层的节点个数+求二叉树叶子节点的个数

size_t _FindLeafSize(Node* root)     //求二叉树叶子节点的个数    {        //static size_t count = 0;        if (root == NULL)            return 0; if (root->_left == NULL&&root->_right == NULL);        return 1; return _FindLeafSize(root->_right) +

二叉树(8)----求二叉树第K层的节点数和二叉树第K层的叶子节点数,递归方式

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.求二叉树第K层的节点数 (1)递归方式 给定根节点pRoot: 如

求二叉树第K层的叶子节点的个数(假设根节点是第一层)

算法思想:采用队列结构按层次遍历,遍历K层时记录叶子的个数 int LeafKlevel(BiTree bt, int k){ //求二叉树bt的第k(k >1)层上叶子的节点个数 if(bt == NULL || k < 1) return 0; BiTree p=bt,Q[]; //Q是队列,元素是二叉树节点的指针 int front = 0,rear = 1,leaf = 0 //front 和 rear 是队头和队尾指针,leaf是叶子节点数 int last = 1,level =

求二叉树第k层的结点个数

tag: 二叉树 - 层次遍历 思路: 用层次遍历思路求解 辅助: 队列 package com.zhaochao.tree; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; /** * Created by zhaochao on 17/1/23. */ public class NodesNumberKLevel { public int getNodesNumberKLevel

求二叉树的高度,叶子节点个数,第K层结点个数,求祖先结点问题

一.求二叉树的高度 类似归并排序的思想.先求最底层结点的高度,再分别比较生成更高的结点的高度.最后递归至根结点,求出根结点的高度. //求二叉树的高度 int Height() { return GetHeight(_root); } int GetHeight(Node<T> *root) { int left = 0; int right = 0; if (root->_leftchild != NULL) left += GetHeight(root->_leftchild)

hdu 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=2665 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number of the test cases. For each test case, the first line contain two integer n and m (

二叉树的层序遍历、二叉树叶节点输出算法、求二叉树的高度、层序创建一棵二叉树

二叉树的层序遍历 1 void LevelorderTraversal(BinTree BT) 2 { 3 std::queue<BinTree> Queue; 4 BinTree T; 5 if (!BT) 6 return; //若是空树则直接返回 7 Queue.push(BT); 8 while (!Queue.empty()) 9 { 10 T = Queue.front(); 11 Queue.pop(); 12 printf("%c ", T->Data

算法学习 - 求二叉树的宽度

二叉树的宽度 二叉树的宽度定义为 整个二叉树各层节点数,其中最大的值为这个二叉树的宽度. 所以二叉树的第一层就是1(根节点). 代码实现(C++) 代码实现比较简单,树的遍历一般用递归比较方便. // // main.cpp // TreeWidth // // Created by Alps on 15/3/11. // Copyright (c) 2015年 chen. All rights reserved. // // achieve the binary tree width. #in

C++算法之 求二叉树的节点个数、深度、四种遍历方法

//节点的数据结构 class BTree { public: int m_nValue; BTree* m_nLeft; BTree* m_nRight; public: BTree(int value) { m_nValue = value; } }; 一:求二叉树的节点个数: /* 求二叉数中的节点个数 递归解法: 1:如果二叉树为空,节点的个数为0 2:如果二叉树不为空,二叉树节点的个数 = 左子树节点个数+右子树节点的个数+1: */ int GetNodeCount(BTree* p