1. Brief Introduction
Problem 1: Given a binary tree, you must traverse the binary tree hierarchically, that is, access the binary tree from the top to bottom layers (each layer will output a single row ), each layer requires access from left to right, and nodes are numbered in sequence.
Question 2: Write a function and print the nodes at a certain level of the binary tree (from left to right). The root Node is layer 0th, and the function prototype is int PrintNodeAtLevel (Node * root, int level ), 1 is returned for success and 0 is returned for failure.
2. Ideas
When using queues for breadth-first search, the main feature is that if no element in the k-layer is displayed, the queue must have no k + 1 layer elements, in addition, if the k-layer elements are out of the queue, only the k + 1-layer elements in the queue contain all the k + 1-layer elements. Therefore, starting from the first layer, the root node is queued, the number of nodes at the current layer is recorded as 1, and all nodes at this layer are output to the new queue, then, the queue only contains the number of nodes in all the 2nd layers, and so on until the queue is empty.
The two problems mentioned in the briefing are basically the same thing. The second problem is that, on the basis of the first problem, you can determine the number of layers and the position of the current layer.
3. Code
The Code Implementation of the first problem is provided here.
# Include <iostream>
# Include <deque>
Using namespace std;
Struct NODE {
NODE * pLeft;
NODE * pRight;
Int value;
};
Void PrintTreeByLevel (const NODE * root ){
Deque <const node *> store;
Int left_num;
If (! Root)
Return;
Store. push_back (Root );
While (! Store. Empty ()){
Left_num = store. Size (); // number of nodes in the current Layer
While (left_num --> 0 ){
Const node * TMP = store. Front ();
Store. pop_front ();
Cout <TMP-> value <"";
If (TMP-> pleft)
Store. push_back (TMP-> pleft );
If (TMP-> pright)
Store. push_back (TMP-> pright );
}
Cout <Endl;
}
}
Int main (){
Node * array [8];
For (INT I = 0; I <8; I ++ ){
Array [I] = new node;
Array [I]-> value = 1 + I;
Array [I]-> pleft = array [I]-> pright = NULL;
}
Array [0]-> pleft = array [1]; array [0]-> pright = array [2];
Array [1]-> pleft = array [3]; array [1]-> pright = NULL;
Array [2]-> pleft = array [4]; array [2]-> pright = array [5];
Array [4]-> pleft = array [6]; array [4]-> pright = array [7];
Printtreebylevel (array [0]);
Reverseprinttreebylevel (array [0]);
System ("pause ");
Return 0;
}
Output result:
4. scaling problems
In the expansion problem, the last layer should be output first, and then the last layer,..., and the last layer should be output.
The first instinct is to record the number of elements in each layer during the process of traversing according to the previous method. Note that only the elements in the traversal are out of the queue, and whether the traversal at each layer is complete. You need to set more counting variables.
However, in an article in the reference section, we can see that the method of using the dummy element is to insert null in the middle of each layer. Similarly, in traversal, The traversal only does not match, and whether the traversal of each layer ends, you need to set more counting variables.
The Code is as follows:
Void reverseprinttreebylevel (const node * root ){
Deque <const node *> store;
Int Index = 0; // The subscript Of The traversal element and the dummy Element
Int NO = 0; // number of elements that have been traversed
If (! Root ){
Return;
}
Store. push_back (Root );
Index = 0;
While (index <store. Size ()){
Store. push_back (null); // dummy Element
While (store [Index]! = NULL) {// access the current Layer
No ++;
If (store [Index]-> pright)
Store. push_back (store [Index]-> pright );
If (store [Index]-> pleft)
Store. push_back (store [Index]-> pleft );
Index ++;
}
Index ++;
}
For (INT I = store. Size ()-1; I> = 0; I --){
If (store [I] = NULL)
Cout <endl;
Else {
Cout <store [I]-> value <"";
}
}
Cout <endl;
}
The output result is as follows:
5. Reference
The beauty of programming, section 3.10, layered traversal of Binary Trees
The beauty of programming: layered traversal Binary Tree http://www.cnblogs.com/DiaoCow/archive/2010/06/01/1749187.html