Binary Tree sequence Traversal

Source: Internet
Author: User

Microsoft interview questions are described as follows:

Enter a binary tree, print each node of the tree from top to bottom, and print the tree from left to right in the same layer.
For example, enter

8
/\
6 10
/\/\
5 7 9 11
Output 8 6 10 5 7 9 11.


Logical Analysis:

1. It is clearly a binary tree sequence traversal. When studying the data structure course, most people are familiar with the first, middle, and back order traversal. All these three are deep searches, however, sequential traversal is a wide search. Even so, Microsoft uses this as an interview subject and it is still quite inexpensive for interviewers.

2. For sequence traversal, similar to the idea of first-order traversal, we know that the implementation of the first-order traversal cycle needs to be achieved through the auxiliary stack. the stack is a LIFO data structure, that is, the process is the opposite for sequence traversal: FIFO, first in first out. Each time we put the root node into the queue, when the queue is not empty, the front is assigned to the temporary pointer temp, and then the satellite domain of the root node is output. Then, the system checks whether the left and right children of the root node are empty. If the queue is not empty, the system enters the queue, after the above work, the elements are retrieved from the queue again. If the queue is empty, the loop ends.

void printByLevel(BSTreeNode* root){queue
 
   Q;BSTreeNode* p = root;if(p == NULL)return ;Q.push(p);while(!Q.empty()){p = Q.front();Q.pop();cout << p->m_nValue << endl;if(p->m_pLeft)Q.push(p->m_pLeft);if(p->m_pRight)Q.push(p->m_pRight);}}
 


3. Because we used BSTree's image flip example, we will not rewrite it to BTree for convenience. After all, the binary sorting tree is also a binary tree. The complete code is as follows:

#include 
 
  #include 
  
   using namespace std;typedef struct BSTreeNode          //a node in the BST  {      int m_nValue; //Value of node      struct BSTreeNode *m_pLeft;    //left child of node      struct BSTreeNode *m_pRight;   //right child of node  }BSTreeNode;  void printByLevel(BSTreeNode* root){queue
   
     Q;BSTreeNode* p = root;if(p == NULL)return ;Q.push(p);while(!Q.empty()){p = Q.front();Q.pop();cout << p->m_nValue << endl;if(p->m_pLeft)Q.push(p->m_pLeft);if(p->m_pRight)Q.push(p->m_pRight);}}int main(){BSTreeNode node[7];      node[0].m_nValue = 8;      node[0].m_pLeft = &node[1];      node[0].m_pRight = &node[2];      node[1].m_nValue = 6;      node[1].m_pLeft = &node[3];      node[1].m_pRight = &node[4];      node[2].m_nValue = 10;      node[2].m_pLeft = &node[5];      node[2].m_pRight = &node[6];      node[3].m_nValue = 5;      node[3].m_pLeft = NULL;      node[3].m_pRight = NULL;      node[4].m_nValue = 7;      node[4].m_pLeft = NULL;      node[4].m_pRight = NULL;      node[5].m_nValue = 9;      node[5].m_pLeft = NULL;      node[5].m_pRight = NULL;      node[6].m_nValue = 11;      node[6].m_pLeft = NULL;      node[6].m_pRight = NULL; printByLevel(&node[0]);return 0;}
   
  
 




Note: For convenience, I borrowed the C ++ STL queue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.