Left Fortune law books "Programmer Code Interview Guide" --3_02 print binary boundary nodes [★★]

[Title]
given node of a binary tree head head, according to the following two criteria were achieved boundary node binary counter printing.
Standard:
1. The head node is a boundary node.
2. leaf node is the border node.
3. If a node in the layer it is the leftmost or rightmost, then the leather is also the border point.
Two criteria:
1. The head node is the border node.
2. leaf node is the border node.
3. The tree path extending down the left edge of a boundary node.
4. The tree path extending down the right boundary as a boundary node.
For example, FIG. 3 - FIG. 2 tree.

 

 

A standard print result is: 1,2,4,7,11,13,14,15,16,12,10,6,3
according to the results print standard two is: 1,2,4,7,13 , 14,15,16,10,6,3
[required]
1. If the number of nodes is N, the time complexity requirements of both standards are implemented is O (N), additional space complexity requirements are O (h) , h is the height of the binary tree.
2. The two standards are not repeated in claim counterclockwise in order to print all the boundary nodes.
[Solution]
implemented according to the requirements of a specific standard printing process is as follows:
1. The resulting binary tree each layer of the leftmost and rightmost node. Topic of example, this record is as follows:
the left most and right most node of nodes
of the first layer 11
second layer 23
a third layer 46
a fourth layer 710
fifth layer 1112
sixth layer 13 16
2 from on to the next print all layers of the leftmost node. Examples of topics for that print: 1,2,4,7,11,13.
3. preorder binary tree, prints those nodes that do not belong to a layer leftmost or rightmost, but they are also nodes leaf nodes. For example of the subject, i.e. printing: 14,15.
4. Print all layers from bottom to top in the right-most node, but the node can not both the most left node, right node is the most. Examples of topics for that print: 16,12,10,6,3.
//////////// Standard /////////////////

. 1  int getHeight (the Node H *, int L) // Get binary layers 
2  {
 . 3  IF (H == nullptr a)
 . 4  return L;
 . 5  return max (getHeight (H-> L, L + . 1 ), getHeight (H -> R & lt, L + . 1 ));
 . 6  }
 . 7  void setEdgeMap (the Node H *, int L, Vector <Vector <* >> the Node & edgeMap)
 . 8  {
 . 9  IF (H == nullptr a)
 10  return ;
 . 11 edgeMap [ L] [ 0 ] = edgeMap [L] [ 0] == nullptr a H: edgeMap [L] [? 0 ];
 12 is edgeMap [L] [ . 1 ] = H;
 13 is setEdgeMap (H-> L, L + . 1 , edgeMap); // find the left boundary of the left 
14 setEdgeMap (H-> R & lt, L + . 1 , edgeMap); // find the right boundary of the right 
15  }
 16  void printLeafEdge (the Node H *, int L, Vector <Vector <* >> the Node edgeMap)
 . 17  {
 18 is  IF (H = = nullptr a)
 . 19  return ;
 20 is  IF (H-> H- && nullptr a L ==> H = R & lt edgeMap && == nullptr a [L] [! 0] && edgeMap H = [L] [! . 1 ]) // is a leaf node, but not about the boundary node 
21 is COUT << H-> V << "  " ;
 22 is printLeafEdge (H-> L, L + . 1 , edgeMap ); // Looking down the boundary leaf node 
23 is printLeafEdge (H-> R & lt, L + . 1 , edgeMap);
 24  }
 25  void printEdge1 (the node * the root)
 26 is  {
 27  IF (the root == nullptr a)
 28  return ;
 29  int H = getHeight (the root, 0 ); // get depth 
30Vector <Vector <* >> edgeMap the Node (H, Vector <* the Node> ( 2 )); // left and right boundaries of each layer 
31 is setEdgeMap (the root, 0 , edgeMap);
 32  // prints left margin 
33 is  for ( int I = 0 ; I <edgeMap.size (); ++ I)
 34 is COUT << edgeMap [I] [ 0 ] -> V << "  " ;
 35  // print is not left and right boundaries, but the boundary nodes 
36 printLeafEdge (the root, 0 , edgeMap);
 37 [  // print right border node 
38 is  for ( int I = edgeMap.size () - . 1; i >= 0; --i)
39 if (edgeMap[i][0] != edgeMap[i][1])
40 cout << edgeMap[i][1]->v << " ";
41 }

 

Concrete process printing in accordance with the standard two requirements are as follows:
1. The head node start looking down, as long as both the left and the first child, but also to find the right child node, denoted h, then proceed to step 2. In the process, we looked nodes are printed. Examples of the subject, the printing namely: 1, because the first node directly to meet the requirements, so there is no subsequent printing after the search process directly proceeds to step 2. However, if the binary tree in FIG. 3 - 3 shown, this time the print: 1,2,3. Node 3 Node start from scratch to meet the requirements of a first down. If the binary tree from top to bottom has been found leaf node does not exist to meet the requirements, indicating a binary tree is a rod-like structure, then return directly to the print looked nodes.
2.h left subtree of the first printing process proceeds to step 3; H right subtree printing process then proceeds to step 4; and finally returns.
3. Print path extending left subtree h and left edge of the leaf nodes of all, see printLeftEdge particular method.
4. Print paths extending the right boundary and the right subtree h all leaf nodes, see printRightEdge particular method.
//////////// standard two /////////////////

 1 void printLeftEdge(Node* h, bool f)
 2 {
 3 if (h == nullptr)
 4 return;
 5 if (f || (h->l == nullptr && h->r == nullptr))//边界叶子节点
 6 cout << h->v << " ";
 7 printLeftEdge(h->l, f);
 8 printLeftEdge(h->r, f && h->l == nullptr ? true : false);
 9 }
10 void printRightEdge(Node* h, bool f)
11 {
12 if (h == nullptr)
13 return;
14 printLeftEdge(h->l, f && h->r == nullptr ? true : false);
15 printLeftEdge(h->r, f);
16 if (f || (h->l == nullptr && h->r == nullptr))//边界叶子节点
17 cout << h->v << " ";
18 }
19 void printEdge2(Node* root)
20 {
21 if (root == nullptr)
22 return;
23 cout << root->v << " " ; // root meet the 
24  IF (directory root-> L = nullptr a && directory root-> = R & lt!! Nullptr a)
 25  {
 26 is printLeftEdge (directory root-> L, to true );
 27 printRightEdge (directory root-> R & lt, to true ) ;
 28  }
 29  the else 
30 printEdge2 (! directory root-> L = nullptr a directory root-> L: directory root->? R & lt);
 31 is  
32 }

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11450437.html