15,012 fork Tree maximum width and height
time limit: 1 sspace limit: 128000 KBtitle level: Silver SolvingView Run ResultsTitle Description
Description
Give a binary tree, output its maximum width and height.
Enter a description
Input Description
The first line is an integer n.
The following n rows have two numbers per row, and for the two numbers of line I, the number of the two or so sons to which the node numbered I is connected. If no son is empty, it is 0.
Output description
Output Description
Output a total row, the maximum width and height of the output binary tree, separated by a space.
Sample input
Sample Input
5
2 3
4 5
0 0
0 0
0 0
Sample output
Sample Output
2 3
Data range and Tips
Data Size & Hint
N<16
The default first is the root node
Numbered in the order in which they were entered
2-n+1 refers to the left child and right child of this node
Note: The second question has extreme data!
1
0 0
You don't want to cut the problem, give me an honest search!
Category labels
Tags Click here to expand
#include <cstdio>#include<iostream>using namespacestd;inta[ -][3],b[ -];intMain () {intN,x,y,k,wide=0, deep=0; scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d%d",&x,&y); a[i][0]=x;a[i][1]=y;//Store left and right childrena[x][2]=i;a[y][2]=i;//store its parent node } for(intI=1; i<=n;i++) {k=1; X=a[i][2]; while(x!=0) {k++; X=a[x][2]; } B[k]++; if(K>deep) deep=K; if(b[k]>wide) wide=B[k]; } printf ("%d%d\n", Wide,deep); //wide--the width of the bottom layer (a node in the upper layer has a maximum of several children); deep--depth return 0;}
Review the two-pronged tree
The average tree can be converted to a two-fork tree, and the storage structure and operation of the binary tree are simpler, so we first introduce the two-fork tree type in the tree.
A binary tree is a finite set of n nodes, which are either empty sets (n=0), or have a root node and a maximum of two disjoint Saozi, called the root of the two-tree of the right subtree.
There are five basic forms of the Binary tree: (1) The left and right subtree of the empty set (2) root is null (3) The tree of the root is empty (4) of the root of the tree is empty (5) the root of the tree isnot empty.
The logical structure of a binary tree:
The binary tree differs from the unordered tree in that each node can have a maximum of two subtrees trees and no left or right points. In addition, the binary tree is different from the ordered tree with the degree of 2, in an ordered tree, although there is a left and right order between the children of a pole, if there is only one child, there is no need to distinguish the order of the left and in the other, and even a child in a binary tree has about. Therefore, the binary tree is not a special case of the tree.
The special nature of the binary tree (not shown here):
(1) The number of nodes on the first layer of the binary tree is 2i-1 (i>=1);
(2) The two-tree with a depth of K has a maximum of 2k-1 nodes (k>=1);
(3) In any binary tree, if the number of terminal nodes is N0, the degree of 2 of the node is N2, then n0=n2+1;
Complete binary tree : If the height of the binary tree is H, except for the H layer, the nodes of each layer (1-H-1) reach the maximum number, and the nodes on the bottom layer are concentrated in several positions on the leftmost side of the layer, then this binary tree is called a complete binary tree.
full two fork Tree : A two-prong tree with a depth of K and a 2k-1 node is called a full two-tree.
There are many properties that are not discussed much in this. You can refer to other books.
Sequential storage structure implementation of the main idea: the general binary tree filled with some empty nodes, so that it becomes a "complete binary tree", and according to the full binary tree form to the node number. The empty nodes are wasted space in sequential stored procedures.
Reference: http://www.360doc.com/content/13/0422/18/3777348_280168665.shtml
Http://www.jb51.net/article/37334.htm
15,012 fork Tree Maximum width and height