您的位置:首页 > Web前端

剑指offer 61 - 按之字形打印二叉树

2015-06-05 16:17 369 查看
题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,即第一行按照从左到右的顺序打印,第二层按照从右到左顺序打印,第三行再按照从左到右的顺序打印,其他以此类推。

按之字形顺序打印二叉树需要两个栈。我们在打印某一行结点时,把下一层的子结点保存到相应的栈里。如果当前打印的是奇数层,则先保存左子结点再保存右子结点到一个栈里;如果当前打印的是偶数层,则先保存右子结点再保存左子结点到第二个栈里。

#include<iostream>
#include<stack>
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 ;
int current=0;
int next=1;
stack<BinaryTreeNode*> nodes[2];
nodes[current].push(pRoot);

while(!nodes[current].empty())
{
BinaryTreeNode* node = nodes[current].top(); //打印当前栈
nodes[current].pop();
cout<<node->m_nValue<<" ";
if(current==0) //偶数行
{
if(node->m_pLeft)
nodes[next].push(node->m_pLeft); //下一行存在另一个栈内
if(node->m_pRight)
nodes[next].push(node->m_pRight);
}
else
{
if(node->m_pRight)
nodes[next].push(node->m_pRight);
if(node->m_pLeft)
nodes[next].push(node->m_pLeft);
}

if(nodes[current].empty()) //一层打印完
{
cout<<endl;
current = 1-current; //交换
next = 1-next;
}

}
}
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);
}
}

// 8
// 6 10
// 5 7 9 11
void Test1()
{
BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

ConnectTreeNodes(pNode8, pNode6, pNode10);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11);

printf("====Test1 Begins: ====\n");
printf("Expected Result is:\n");
printf("8 \n");
printf("10 6 \n");
printf("5 7 9 11 \n\n");

printf("Actual Result is: \n");
Print(pNode8);
printf("\n");

DestroyTree(pNode8);
}

// 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,
4000
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");
Print(pNode5);
printf("\n");

DestroyTree(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");
Print(pNode5);
printf("\n");

DestroyTree(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");
Print(pNode5);
printf("\n");

DestroyTree(pNode5);
}

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

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

// 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");
Print(pNode100);
printf("\n");
}

// 8
// 4 12
// 2 6 10 14
// 1 3 5 7 9 11 13 15
void Test7()
{
BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
BinaryTreeNode* pNode13 = CreateBinaryTreeNode(13);
BinaryTreeNode* pNode15 = CreateBinaryTreeNode(15);

ConnectTreeNodes(pNode8, pNode4, pNode12);
ConnectTreeNodes(pNode4, pNode2, pNode6);
ConnectTreeNodes(pNode12, pNode10, pNode14);
ConnectTreeNodes(pNode2, pNode1, pNode3);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11);
ConnectTreeNodes(pNode14, pNode13, pNode15);

printf("====Test7 Begins: ====\n");
printf("Expected Result is:\n");
printf("8 \n");
printf("12 4 \n");
printf("2 6 10 14 \n");
printf("15 13 11 9 7 5 3 1 \n\n");

printf("Actual Result is: \n");
Print(pNode8);
printf("\n");

DestroyTree(pNode8);
}

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

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: