1102. Invert a Binary Tree
来源:互联网 发布:端口号占用查询 编辑:程序博客网 时间:2024/06/07 05:22
The following is from Max Howell @twitter:
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.
Now it's your turn to prove that YOU CAN invert a binary tree!
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node from 0 to N-1, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:81 -- -0 -2 7- -- -5 -4 6Sample Output:
3 7 2 6 4 0 5 16 5 7 4 3 2 0 1
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>#include <queue>using namespace std;struct node{int val;int left,right;node():left(-1),right(-1){}};vector<node>tree;void invert(int cur){if(cur==-1) return;swap(tree[cur].left,tree[cur].right);invert(tree[cur].left);invert(tree[cur].right);}void levelorder(int root){queue<int>que;que.push(root);bool flg=true;while(!que.empty()){int tmp=que.front();que.pop();if(flg){cout<<tmp;flg=false;}else{cout<<" "<<tmp;}if(tree[tmp].left!=-1) que.push(tree[tmp].left);if(tree[tmp].right!=-1) que.push(tree[tmp].right);}}bool flg1=true;void inorder(int root){if(root!=-1){inorder(tree[root].left);if(flg1){cout<<root;flg1=false;}else{cout<<" "<<root;}inorder(tree[root].right);}}int main(){int n;cin>>n;tree.resize(n);int root;vector<bool>isroot(n,true);for(int i=0;i<n;i++){tree[i].val=i;char l,r;cin>>l>>r;if(l!='-') {tree[i].left=l-'0';isroot[l-'0']=false;}if(r!='-') {tree[i].right=r-'0';isroot[r-'0']=false;}}for(int i=0;i<n;i++){if(isroot[i]) {root=i;break;}}//cout<<root;invert(root);levelorder(root);cout<<endl;inorder(root);}
- 1102. Invert a Binary Tree
- 1102. Invert a Binary Tree
- 1102. Invert a Binary Tree
- 1102. Invert a Binary Tree
- 1102. Invert a Binary Tree
- Invert a binary tree
- Invert a binary tree.
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- [PAT]1102. Invert a Binary Tree (25)
- 【PAT】1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- Java中ArrayQueue类的使用
- 结构体A里某个变量相对A的偏移量
- POJ 3369 Grocery store 可能会
- ssm框架下,前台与后台的数据交互
- prim算法求最小生成树的最大边--poj2253
- 1102. Invert a Binary Tree
- JTable
- 在Jupyter notebook中配置和使用spark
- iOS 21种设计模式之单例(Singleton)
- 深度神经网络(DNN)模型与前向传播算法
- 临时记录
- 1103. Integer Factorization
- pat-top 1004. To Buy or Not to Buy
- jenkins+gitlab 安装和配置