1102. Invert a Binary Tree (25)

原创 2015年11月20日 22:15:04

1.和leetcode中的Invert Binary Tree(easy)题目差不多

2.利用递归函数,每次都把左右子树翻转即可

函数如下:

void invertTree(TreeNode* root)
{
	if (root != NULL)
	{
		swap(root->l, root->r);
		invertTree(root->l);
		invertTree(root->r);
	}
	
}


AC代码如下:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
struct TreeNode{
	int val;
	TreeNode*l, *r;
	TreeNode() :val(-1),l(NULL), r(NULL){};
};
void invertTree(TreeNode* root)
{
	if (root != NULL)
	{
		swap(root->l, root->r);
		invertTree(root->l);
		invertTree(root->r);
	}
	
}
void levelOrder(TreeNode*root,vector<int>&ans)
{
	queue<TreeNode*> q;
	if (root != NULL)
	{
		q.push(root);
		int count1 = 1;
		int count2 = 0;
		while (!q.empty())
		{
			for (int i = 0; i < count1; i++)
			{
				TreeNode* head = q.front(); q.pop();
				ans.push_back(head->val);
				if (head->l != NULL)
				{
					q.push(head->l);
					count2++;
				}
				if (head->r != NULL)
				{
					q.push(head->r);
					count2++;
				}
			}
			count1 = count2;
			count2 = 0;
		}

	}
}
void inOrder(TreeNode*root, vector<int>&ans)
{
	if (root != NULL)
	{
		inOrder(root->l, ans);
		ans.push_back(root->val);
		inOrder(root->r, ans);
	}
}
int main(void)
{
	
	int sum;
	cin >> sum;
	vector<TreeNode> tree(sum);
	vector<int> degree(sum,0);
	for (int i = 0; i < sum; i++)
	{
		tree[i].val = i;
		char a, b;
		cin >> a >> b;
		if (a!='-')
		{
			tree[i].l = &tree[a - '0'];
			degree[a - '0']++;
		}
		if (b != '-')
		{
			tree[i].r = &tree[b - '0'];
			degree[b - '0']++;
		}
	}
	TreeNode* root=NULL;
	for (int i = 0; i < sum; i++)
	{
		if (degree[i] == 0)
		{
			root = &tree[i];
			break;
		}
	}
	invertTree(root);
	vector<int> ans1(0);
	vector<int> ans2(0);
	levelOrder(root, ans1);
	inOrder(root, ans2);
	for (int i = 0; i < ans1.size(); i++)
	{
		cout << ans1[i];
		if (i != ans1.size() - 1)
			cout << " ";
	}
	cout << endl;
	for (int i = 0; i < ans2.size(); i++)
	{
		cout << ans2[i];
		if (i != ans2.size() - 1)
			cout << " ";
	}
	cout << endl;
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

1102. Invert a Binary Tree (25)

唯一注意一点,就是根结点这里并未给出,需要自己根据输入判断出来。 #include #include #include #include #include #include usi...

1102. Invert a Binary Tree (25)-PAT甲级真题

1102. Invert a Binary Tree (25) The following is from Max Howell @twitter: Google: 90% of ou...

A1102. Invert a Binary Tree (25)

A1102. Invert a Binary Tree (25)

PAT (Advanced Level) 1102. Invert a Binary Tree (25) 解题报告

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ...

PAT甲 1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrot...

1102. Invert a Binary Tree (25)

提交情况:代码:#include #include #include using namespace std; #define N 11 typedef struct Node { i...

PAT A1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrot...

1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrot...

PAT - 甲级 - 1102. Invert a Binary Tree (25)(中序遍历,层次遍历)

题目描述: The following is from Max Howell @twitter: Google: 90% of our engineers use the software y...

PAT 1102. Invert a Binary Tree (25)

层次遍历先输出右节点 (原来以为是先输出大的 提交以后有一个错误 测试点1 仔细看过代码后发现没错误就觉得是这个问题 一试就成功) #include #include #include #includ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)