剑指offer 60 - 把二叉树打印成多行

题目:  按层打印二叉树,每一层打印到一行。

8

6  10

5     7    9   11

类似于面试题23,用一个队列保存将要打印的节点,为了将二叉树的每一行打印一行里,我们需要两个变量,一个变量表示在当前层中还没有打印的节点数,另一个变量表示下一层节点的数目。

变量toBePrinted表示在当前层中还有没有打印的节点数,而变量nextLevel比欧式下一层的节点数。如果一个节点有子节点,我们把每一个子节点加到队列中,同时把变量nexLevel加1.每当我们打印一个节点,toBePeinted减1,当toBePrnted编程0时,表示当前层所有节点已经打印完了,可以继续打印下一层。

#include<iostream>
#include<queue>
using namespace std;

struct BinaryTreeNode
{
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

BinaryTreeNode*		CreateBinaryTreeNode(int value)
{
	BinaryTreeNode*	node = new BinaryTreeNode[sizeof(BinaryTreeNode)];
	node->m_nValue = value;
	node->m_pLeft = node->m_pRight	=	NULL;
	return node;
}

void ConnectTreeNodes(BinaryTreeNode*	pRoot,BinaryTreeNode*	pLeft,BinaryTreeNode*	pRight)
{
	if(pRoot!=NULL)
	{
		pRoot->m_pLeft	=	pLeft;
		pRoot->m_pRight	=	pRight;
	}
}

void print(BinaryTreeNode*	pRoot)
{
	if(pRoot ==	NULL)
		return ;
	queue<BinaryTreeNode*> nodes;
	nodes.push(pRoot);
	int toBePrinted=1;
	int nextLevel=0;
	while(nodes.size()>0)
	{
		BinaryTreeNode*	node = nodes.front();
		nodes.pop();
		cout<<node->m_nValue<<" ";
		--toBePrinted;

		if(node->m_pLeft)
		{
			nodes.push(node->m_pLeft);
			++nextLevel ;
		}
		if(node->m_pRight)
		{
			nodes.push(node->m_pRight);
			++nextLevel ;
		}

		if(toBePrinted==0)
		{
			cout<<endl;
			toBePrinted= nextLevel ;
			nextLevel =0;
		}

	}
}
void DestroyTree(BinaryTreeNode*	pRoot)
{
	if(	pRoot!=NULL)
	{
		BinaryTreeNode* pLeft = pRoot->m_pLeft;  
        BinaryTreeNode* pRight = pRoot->m_pRight;  
  
        delete pRoot;  
        pRoot = NULL;  
  
        DestroyTree(pLeft);  
        DestroyTree(pRight);  
	}
}
void Test(BinaryTreeNode* pRoot)
{
	print(pRoot);
	cout<<endl;
	DestroyTree(pRoot);
}
//            10  
//         /      \  
//        6        14  
//       /\        /\  
//      4  8     12  16     
void Test1()
{
	BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);  
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);  
    BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);  
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);  
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);  
    BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);  
    BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);  

	ConnectTreeNodes(pNode10, pNode6, pNode14);  
    ConnectTreeNodes(pNode6, pNode4, pNode8);  
    ConnectTreeNodes(pNode14, pNode12, pNode16);
	
	 printf("====Test1 Begins: ====\n");
    printf("Expected Result is:\n");
    printf("10 \n");
    printf("6 14 \n");
    printf("4 8 12 16 \n\n");
	printf("Actual Result is: \n");
	Test(pNode10);
}
//            5
//          4
//        3
//      2
void Test2()
{
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);

    ConnectTreeNodes(pNode5, pNode4, NULL);
    ConnectTreeNodes(pNode4, pNode3, NULL);
    ConnectTreeNodes(pNode3, pNode2, NULL);
	   
	printf("====Test2 Begins: ====\n");
    printf("Expected Result is:\n");
    printf("5 \n");
    printf("4 \n");
    printf("3 \n");
    printf("2 \n\n");

	printf("Actual Result is: \n");
    Test(pNode5);
}

//        5
//         4
//          3
//           2
void Test3()
{
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);

    ConnectTreeNodes(pNode5, NULL, pNode4);
    ConnectTreeNodes(pNode4, NULL, pNode3);
    ConnectTreeNodes(pNode3, NULL, pNode2);

    printf("====Test3 Begins: ====\n");
    printf("Expected Result is:\n");
    printf("5 \n");
    printf("4 \n");
    printf("3 \n");
    printf("2 \n\n");

    printf("Actual Result is: \n");
    Test(pNode5);
}

void Test4()
{
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

    printf("====Test4 Begins: ====\n");
    printf("Expected Result is:\n");
    printf("5 \n\n");

    printf("Actual Result is: \n");
    Test(pNode5);
}

void Test5()
{
    printf("====Test5 Begins: ====\n");
    printf("Expected Result is:\n");

    printf("Actual Result is: \n");
    Test(NULL);
}

//        100
//        /
//       50   
//         \
//         150
void Test6()
{
    BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100);
    BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50);
    BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150);

    ConnectTreeNodes(pNode100, pNode50, NULL);
    ConnectTreeNodes(pNode50, NULL, pNode150);

    printf("====Test6 Begins: ====\n");
    printf("Expected Result is:\n");
    printf("100 \n");
    printf("50 \n");
    printf("150 \n\n");

    printf("Actual Result is: \n");
  Test(pNode100);
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值