1102 Invert a Binary Tree (25 分)
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:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
//!!考点1:主要问题在于如何找到根结点,很明显根结点不是任何结点的子树,可以定义
//一个长度为N的bool类型数组child,元素均初始化为false,然后在读取子
//树结点时,将child数组相应位置的元素设为true,读取完成后遍历整个child
//数组,元素仍为false的下标即为根结点的编号。
typedef struct node
{
int data,lchild=-1,rchild=-1;
}node;
vector<node>tree(1005);
void levelTraval(int root)
{
queue<int>q;
q.push(root);
int flag=0;
while(!q.empty())
{
int tmp=q.front();
q.pop();
if(flag==0)
{
flag=1;
printf("%d",tmp);
}
else
printf(" %d",tmp);
if(tree[tmp].lchild!=-1) q.push(tree[tmp].lchild);
if(tree[tmp].rchild!=-1) q.push(tree[tmp].rchild);
}
}
void InOrder(int root,bool &space) //考点2:中根遍历,space指示输出数字之前是否需要输出空格!!
{
if(root==-1)
return;
InOrder(tree[root].lchild,space);
if(space==true)
{
printf("%d",root);
space=false;
}
else
printf(" %d",root);
InOrder(tree[root].rchild,space);
}
int main(){
int n;
scanf("%d",&n);
getchar();
bool child[10001]={false};
for(int i=0;i<n;i++)
{
char a,b;
scanf("%c %c",&a,&b);
getchar();
if(a!='-')
{
int x1=a-'0';
// printf("%d ",x1);
tree[i].rchild=x1;
child[x1]=true;
}
if(b!='-')
{
int x2=b-'0';
// printf("%d ",x2);
tree[i].lchild=x2;
child[x2]=true;
}
//printf("%c %c",a,b);
}
int root;
for(int i=0;i<n;i++)
{
if(child[i]==false)
root=i;
}
levelTraval(root);
printf("\n");
bool space=true;
InOrder(root,space);
}
1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))
One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.
Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.
Output Specification:
For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.
Finally print in a line Max Heap
if it is a max heap, or Min Heap
for a min heap, or Not Heap
if it is not a heap at all.
Sample Input 1:
8
98 72 86 60 65 12 23 50
Sample Output 1:
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
Sample Input 2:
8
8 38 25 58 52 82 70 60
Sample Output 2:
8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap
Sample Input 3:
8
10 28 15 12 34 9 8 56
Sample Output 3:
10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
int a[1001];
vector<int>path;
int n;
void DFS(int v,bool maxHeap,bool &isHeap)
{
if(v>n) //!!
return;
path.push_back(a[v]);
int tmp1=2*v;
int tmp2=2*v+1;
if(tmp1>n) //叶节点
{
for(int i=0;i<path.size();i++)
{
if(i==0)
printf("%d",path[i]);
else
printf(" %d",path[i]);
}
printf("\n");
path.pop_back(); //!!弹出最后一个节点
return;
}
if(((tmp2<=n)&&(maxHeap^a[v]>a[tmp2]))||(tmp1<=n&&(maxHeap^a[v]>a[tmp1]))) //判断最大堆还是最小堆的方法!!
{
isHeap=false;
}
DFS(tmp2,maxHeap,isHeap);
DFS(tmp1,maxHeap,isHeap);
path.pop_back(); //!!弹出最后一个节点
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
bool isHeap=true; //现根据1,2节点判断是最大堆还是最小堆
DFS(1,a[1]>a[2],isHeap); //把要判断的放在括号里
if(isHeap)
{
if(a[1]>a[2])
printf("Max Heap");
else if(a[1]<a[2])
printf("Min Heap");
}
else
printf("Not Heap");
}
1115 Counting Nodes in a BST (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000) which is the size of the input sequence. Then given in the next line are the N integers in [−10001000] which are supposed to be inserted into an initially empty binary search tree.
Output Specification:
For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:
n1 + n2 = n
where n1
is the number of nodes in the lowest level, n2
is that of the level above, and n
is the sum.
Sample Input:
9
25 30 42 16 20 20 35 -5 28
Sample Output:
2 + 4 = 6
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef struct node
{
int data;
node *lchild,*rchild;
}node;
vector<node>tree[1001];
map<node*,int>mmap;
int max1=0,cnt=0,cnt1=0;
node *insert(node *root,int d)
{
if(root==NULL)
{
root=(node*)malloc(sizeof(node));
root->data=d;
root->lchild=root->rchild=NULL;
return root;
}
else if(d<=root->data)
{
root->lchild=insert(root->lchild,d);
}
else if(d>root->data)
{
root->rchild=insert(root->rchild,d);
}
return root;
}
void preTraval(node *root)
{
if(root)
{
if(mmap[root]>=max1)
{
max1=mmap[root];
//printf("%d ",root->data);
}
// printf("%d",mmap[root]);
preTraval(root->lchild);
preTraval(root->rchild);
}
}
void inTraval(node *root)
{
if(root)
{
// printf("%d",mmap[root]);
inTraval(root->lchild);
if(mmap[root]==max1)
{
cnt++;
}
if(mmap[root]==max1-1)
{
cnt1++;
}
inTraval(root->rchild);
}
}
void levelTraval(node *root)
{
queue<node*>q;
q.push(root);
vector<node>v;
mmap[root]=1;
while(!q.empty())
{
node *tmp=q.front();
// printf("%d ",mmap[tmp]);
q.pop();
if(tmp->lchild)
{
q.push(tmp->lchild);
mmap[tmp->lchild]=mmap[tmp]+1;
}
if(tmp->rchild)
{
q.push(tmp->rchild);
mmap[tmp->rchild]=mmap[tmp]+1;
}
}
}
int main(){
int n,a[1001];
scanf("%d",&n);
node *root=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
root=insert(root,a[i]);
}
levelTraval(root);
preTraval(root);
inTraval(root);
printf("%d + %d = %d\n",cnt,cnt1,cnt+cnt1);
}