Layer-by-layer traversal of Binary Trees, each layer outputs a line

Source: Internet
Author: User

If you just traverse the binary tree hierarchically and print out all the elements, using queues to implement BFS is the best choice.

However, it is required that each layer element be printed as one line, so we must know what the start and end of each layer element is. In this case, it is better to use arrays or vector containers, two variables are used to identify the start and end of each layer and control the access to each layer element. The Code is as follows:

/** Print the binary tree hierarchically, with each row **/# include <iostream> # include <queue> using namespace STD; struct node {int data; node * left; node * Right ;}; vector <node *> VEC; // if it is a common layered traversal binary tree, you only need to print the sequence out/use the queue is the best choice // However, to print each layer of branch, you need to add a flag of the Start // and end, therefore, using a vector or array is more suitable for void travelbylevel (node * root) {If (root = NULL) return; Vec. push_back (Root); int cur = 0; int last = 1; while (cur <Vec. size () {last = Vec. size (); // The start of a layer while (cur <Last) {cout <VEC [cur] <""; if (VEC [cur]-> left! = NULL) {Vec. push_back (VEC [cur]-> left);} If (VEC [cur]-> right! = NULL) {Vec. push_back (VEC [cur]-> right);} cur ++;} cout <Endl ;}}

What if we want to print the nodes from the leaf node to the root layer in layers?

At this point, the above method will not apply, because the above method, while traversing, the vector is also adaptive to the future growth, in line with the self root to the leaf traversal direction.

You can add an identifier between each layer to solve this problem. For example, use null to separate each layer. At the same time, the order in which left and right child nodes are added to the vector determines whether to traverse from left to right or from right to left.

/** Access a binary tree node hierarchically from the bottom up **/# include <iostream> using namespace STD; struct node {int data; node * left; node * right ;}; vector <node *> VEC; // direction controls the sequence of node access from left to right or from right to left void printnodebylevel (node * root, int direction) {If (root = NULL) return; Vec. push_back (Root); int cur = 0; int last; while (cur <Vec. size () {last = Vec. size (); Vec. push_back (null); While (cur <last) {// The access order of each layer is from right to left if (Direction) {If (VEC [C Ur]-> left! = NULL) {Vec. push_back (VEC [cur]-> left);} If (VEC [cur]-> right! = NULL) {Vec. push_back (VEC [cur]-> right);} // The access order of each layer is from left to right} else {If (VEC [cur]-> right! = NULL) {Vec. push_back (VEC [cur]-> right);} If (VEC [cur]-> left! = NULL) {Vec. push_back (VEC [cur]-> left) ;}} cur ++ ;}// skip null cur ++ ;}// reverse traversal, output result for (vector <node *>: iterator iter = Vec. rbegin (); iter! = Vec. rend (); ++ ITER) {If (* iter = NULL) {cout <Endl;} else {cout <(* ITER) -> data <"";}}}

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.