【剑指】32(3).之字形打印二叉树

题目描述
  • 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
算法分析
  • 设两个栈s1和s2,s1存放奇数层,s2存放偶数层;遍历s1节点的同时按照左子树、右子树的顺序加入s2,遍历s2节点的同时按照右子树、左子树的顺序加入s1。

提交代码:

class Solution {
public:
	vector<vector<int> > Print(TreeNode* pRoot) {
		if (!pRoot)
			return vector<vector<int> >();

		vector<vector<int> > result;
		stack<TreeNode*> nodeQue[2];

		nodeQue[0].push(pRoot);
		int layer = 0;

		while (!nodeQue[0].empty() 
			|| !nodeQue[1].empty())
		{
			int elems = nodeQue[layer].size();
			vector<int> temp;

			for (int i = 0; i < elems; ++i)
			{
				TreeNode* node = nodeQue[layer].top();
				nodeQue[layer].pop();

				temp.push_back(node->val);
                                /* 偶数行 */
				if ((layer & 1) == 0)
				{
					if (node->left)
						nodeQue[1].push(node->left);
					if (node->right)
						nodeQue[1].push(node->right);
				}
				else
				{
					if (node->right)
						nodeQue[0].push(node->right);
					if (node->left)
						nodeQue[0].push(node->left);
				}
			}
			layer = layer ^ 1;
			result.push_back(temp);
		}
		return result;
	}
};

测试代码:

// ====================测试代码====================
//            8
//        6      10
//       5 7    9  11
void Test1()
{
	TreeNode* pNode8 = CreateBinaryTreeNode(8);
	TreeNode* pNode6 = CreateBinaryTreeNode(6);
	TreeNode* pNode10 = CreateBinaryTreeNode(10);
	TreeNode* pNode5 = CreateBinaryTreeNode(5);
	TreeNode* pNode7 = CreateBinaryTreeNode(7);
	TreeNode* pNode9 = CreateBinaryTreeNode(9);
	TreeNode* 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");
	Solution s;
	vector<vector<int> > result = s.Print(pNode8);
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
	DestroyTree(pNode8);
}

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

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

	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");
	Solution s;
	vector<vector<int> > result = s.Print(pNode5);
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
	DestroyTree(pNode5);
}

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

	ConnectTreeNodes(pNode5, nullptr, pNode4);
	ConnectTreeNodes(pNode4, nullptr, pNode3);
	ConnectTreeNodes(pNode3, nullptr, 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");
	Solution s;
	vector<vector<int> > result = s.Print(pNode5);
	printf("\n");
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
	DestroyTree(pNode5);
}

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

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

	printf("Actual Result is: \n");
	Solution s;
	vector<vector<int> > result = s.Print(pNode5);
	printf("\n");
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
	DestroyTree(pNode5);
}

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

	printf("Actual Result is: \n");
	Solution s;
	vector<vector<int> > result = s.Print(nullptr);
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
}

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

	ConnectTreeNodes(pNode100, pNode50, nullptr);
	ConnectTreeNodes(pNode50, nullptr, 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");
	Solution s;
	vector<vector<int> > result = s.Print(pNode100);
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
}

//                8
//        4              12
//     2     6       10      14
//   1  3  5  7     9 11   13  15
void Test7()
{
	TreeNode* pNode8 = CreateBinaryTreeNode(8);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode12 = CreateBinaryTreeNode(12);
	TreeNode* pNode2 = CreateBinaryTreeNode(2);
	TreeNode* pNode6 = CreateBinaryTreeNode(6);
	TreeNode* pNode10 = CreateBinaryTreeNode(10);
	TreeNode* pNode14 = CreateBinaryTreeNode(14);
	TreeNode* pNode1 = CreateBinaryTreeNode(1);
	TreeNode* pNode3 = CreateBinaryTreeNode(3);
	TreeNode* pNode5 = CreateBinaryTreeNode(5);
	TreeNode* pNode7 = CreateBinaryTreeNode(7);
	TreeNode* pNode9 = CreateBinaryTreeNode(9);
	TreeNode* pNode11 = CreateBinaryTreeNode(11);
	TreeNode* pNode13 = CreateBinaryTreeNode(13);
	TreeNode* 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");
	Solution s;
	vector<vector<int> > result = s.Print(pNode8);
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
	DestroyTree(pNode8);
}

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

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值