#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <sstream>
using namespace std;
/*
问题:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
分析:逆置一颗二叉树。实际上就是对于一个节点,逆置其左子树,逆置其右子树。
然后另当前孩子的左右孩子互换即可
输入:
7
4 2 7 1 3 6 9
7
4 2 7 1 N N 9
7
4 2 7 1 N N N
输出:
4 7 2 9 6 3 1
4 7 2 9 1
4 7 2 1
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void invertTreeHelper(TreeNode* root) {
if(!root)
{
return ;
}
invertTree(root->left);
invertTree(root->right);
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
}
TreeNode* invertTree(TreeNode* root) {
invertTreeHelper(root);
return root;
}
};
//构建二叉树,这里默认首个元素为二叉树根节点,然后接下来按照作为每个结点的左右孩子的顺序遍历
//这里的输入是每个结点值为字符串,如果字符串的值为NULL表示当前结点为空
TreeNode* buildBinaryTree(vector<string>& nums)
{
if(nums.empty())
{
return NULL;
}
int size = nums.size();
int j = 0;
//结点i的孩子结点是2i,2i+1
vector<TreeNode*> nodes;
int value;
for(int i = 0 ; i < size ; i++)
{
//如果当前结点为空结点,自然其没有左右孩子结点
if("N" == nums.at(i))
{
nodes.push_back(NULL);
continue;
}
value = atoi(nums.at(i).c_str());
TreeNode* node = new TreeNode(value);
nodes.push_back(node);
}
//设定孩子结点指向,各个结点都设置好了,如果但钱为空结点,就不进行指向
for(int i = 1 ; i <= size ; i++)
{
if(NULL == nodes.at(i-1))
{
continue;
}
if(2 * i <= size)
{
nodes.at(i-1)->left = nodes.at(2*i - 1);
}
if(2*i + 1 <= size)
{
nodes.at(i-1)->right = nodes.at(2*i);
}
}
//设定完了之后,返回根节点
return nodes.at(0);
}
void deleteBinaryTree(TreeNode* root)
{
if(!root)
{
return;
}
if(NULL == root->left && NULL == root->right)
{
delete root;
root = NULL;
}
if(root)
{
deleteBinaryTree(root->left);
deleteBinaryTree(root->right);
}
}
//层序遍历
vector<vector<string>> levelOrder(TreeNode* root) {
vector<vector<string>> results;
if(NULL == root)
{
return results;
}
queue<TreeNode*> nodes;
nodes.push(root);
int size = 1;
int nextSize = 0;
vector<string> result;
TreeNode* node = NULL;
while(!nodes.empty())
{
node = nodes.front();
nodes.pop();
if(node)
{
stringstream stream;
stream << node->val;
result.push_back(stream.str());
}
else
{
result.push_back("N");//表示空结点
}
if(node->left)
{
nodes.push(node->left);
nextSize += 1;
}
if(node->right)
{
nodes.push(node->right);
nextSize += 1;
}
size--;
if(0 == size)
{
size = nextSize;
nextSize = 0;
vector<string> tempResult(result);
results.push_back(tempResult);
result.clear();
}
}
return results;
}
void print(vector<vector<string>>& result)
{
if(result.empty())
{
cout << "no result" << endl;
return;
}
int size = result.size();
int len;
for(int i = 0 ; i < size ; i++)
{
len = result.at(i).size();
for(int j = 0 ; j < len ; j++)
{
cout << result.at(i).at(j) << " " ;
}
}
cout << endl;
}
void process()
{
vector<string> nums;
string value;
int num;
Solution solution;
vector<vector<string> > result;
while(cin >> num )
{
nums.clear();
for(int i = 0 ; i < num ; i++)
{
cin >> value;
nums.push_back(value);
}
TreeNode* root = buildBinaryTree(nums);
TreeNode* newRoot = solution.invertTree(root);
//打印二叉树中序结点值
result = levelOrder(root);
print(result);
deleteBinaryTree(newRoot);
}
}
int main(int argc , char* argv[])
{
process();
getchar();
return 0;
}
07-19
07-19
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交