对称的二叉树,按之字形顺序打印二叉树,把二叉树打印成多行(剑指offer58-60)c++版本

#include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
		val(x), left(NULL), right(NULL) {
	}
};

class Solution {
public:
	//JZ58 对称的二叉树
	bool isSymmetrical(TreeNode* pRoot);
	bool isSymmetricalCore(TreeNode* pRoot, TreeNode* pRoot1);
	//JZ59 按之字形顺序打印二叉树
	vector<vector<int> > Print(TreeNode* pRoot);
	//JZ60 把二叉树打印成多行
	vector<vector<int> > Print_1(TreeNode* pRoot);
};

//JZ58 对称的二叉树
bool Solution::isSymmetrical(TreeNode* pRoot) {
	//递归求解,判断左子树的左子节点与右子树的右子结点是否对称
	if (!pRoot)	return true;
	return isSymmetricalCore(pRoot->left, pRoot->right);
}
bool Solution::isSymmetricalCore(TreeNode* pRoot, TreeNode* pRoot1) {
	if (!pRoot && !pRoot1)	return true;
	if (!pRoot || !pRoot1 || pRoot->val != pRoot1->val)	return false;
	return isSymmetricalCore(pRoot->left, pRoot1->right) && isSymmetricalCore(pRoot->right, pRoot1->left);
}
//JZ59 按之字形顺序打印二叉树
vector<vector<int> > Solution::Print(TreeNode* pRoot) {
	//观察发现,按层数遍历是先进后出的,所以可以用栈来实现
	if (!pRoot)	return vector<vector<int> >();
	vector<vector<int> > result;
	stack<TreeNode*> tempstack;
	int count = 0;//count为偶数的时候是从左向右,为奇数的时候是从右向左
	int nums = 1;//记录每层中的节点个数
	tempstack.push(pRoot);
	while (!tempstack.empty()) {
		vector<int> temp;
		stack<TreeNode*> tempstack1;
		TreeNode* tempNode;
		int i = 0;
		int tempnums = nums;
		nums = 0;
		if (count % 2 == 0) {//从左向右
			while (i < tempnums) {
				tempNode = tempstack.top();
				temp.push_back(tempNode->val);
				tempstack.pop();
				if (tempNode->left) {
					tempstack1.push(tempNode->left);
					nums++;
				}
				if (tempNode->right) {
					tempstack1.push(tempNode->right);
					nums++;
				}
				i++;
			}
		}
		else {
			while (i < tempnums) {
				tempNode = tempstack.top();
				temp.push_back(tempNode->val);
				tempstack.pop();
				if (tempNode->right) {
					tempstack1.push(tempNode->right);
					nums++;
				}
				if (tempNode->left) {
					tempstack1.push(tempNode->left);
					nums++;
				}
				i++;
			}
		}
		tempstack = tempstack1;
		count++;
		result.push_back(temp);
	}
	return result;
}
//JZ60 把二叉树打印成多行
vector<vector<int> > Solution::Print_1(TreeNode* pRoot) {
	//队列,和59相似
	if (!pRoot)	return vector<vector<int> >();
	vector<vector<int> >result;
	queue<TreeNode*> que;
	que.push(pRoot);
	int nums = 1;//存储每层的节点个数
	while (!que.empty()) {
		TreeNode* temp = que.front();
		vector<int> tempvec;
		int tempnums = nums;
		nums = 0;
		int i = 0;
		while (i < tempnums) {
			temp = que.front();
			que.pop();
			tempvec.push_back(temp->val);
			if (temp->left) {
				que.push(temp->left);
				nums++;
			}
			if (temp->right) {
				que.push(temp->right);
				nums++;
			}
			i++;
		}
		result.push_back(tempvec);
	}
	return result;
}

//JZ58 对称的二叉树
void test1() {
	TreeNode* pRoot = new TreeNode(1);
	pRoot->left = new TreeNode(2);
	pRoot->left->left = new TreeNode(3);
	pRoot->left->right = new TreeNode(4);
	pRoot->right = new TreeNode(2);
	pRoot->right->left = new TreeNode(4);
	pRoot->right->right = new TreeNode(3);
	Solution s;
	cout << s.isSymmetrical(pRoot);
	return;
}
//JZ59 按之字形顺序打印二叉树
void test2() {
	TreeNode* pRoot = new TreeNode(1);
	pRoot->left = new TreeNode(2);
	pRoot->left->left = new TreeNode(3);
	pRoot->left->right = new TreeNode(4);
	pRoot->right = new TreeNode(2);
	pRoot->right->left = new TreeNode(4);
	pRoot->right->right = new TreeNode(3);
	Solution s;
	vector<vector<int> > result = s.Print(pRoot);
	int len = result.size();
	int i = 0;
	while (i < len) {
		for (vector<int>::iterator it = result[i].begin(); it != result[i].end(); it++) {
			cout << *it << ' ';
		}
		cout << endl;
		i++;
	}
	return;
}
//JZ60 把二叉树打印成多行
void test3() {
	TreeNode* pRoot = new TreeNode(1);
	pRoot->left = new TreeNode(2);
	pRoot->left->left = new TreeNode(4);
	pRoot->left->right = new TreeNode(5);
	pRoot->right = new TreeNode(3);
	pRoot->right->left = new TreeNode(6);
	pRoot->right->right = new TreeNode(7);
	Solution s;
	vector<vector<int> > result = s.Print_1(pRoot);
	int len = result.size();
	int i = 0;
	while (i < len) {
		for (vector<int>::iterator it = result[i].begin(); it != result[i].end(); it++) {
			cout << *it << ' ';
		}
		cout << endl;
		i++;
	}
	return;
}


int main() {
	//test1();
	//test2();
	test3();
	//test4();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值