Leetcode: Binary Tree level order traversal (sequence traversal of a binary tree)

Source: Internet
Author: User

Question:

Given a binary tree, returnLevel orderTraversal of its nodes 'values. (ie, From left to right, level by level).

For example:
Given Binary Tree{3,9,20,#,#,15,7},

    3   /   9  20    /     15   7

 

Return its level order traversal:

[  [3],  [9,20],  [15,7]]

 

Confused what"{1,#,2,3}"Means? > Read more on how binary tree is serialized on OJ.

Note:

1) sequence traversal: each layer is output independently. sequence: from top to bottom, from left to right (think: from bottom to top, from right to left? As described below)

2) implementations are also divided into recursion and iteration, where iteration is labeled with two implementations (the idea is basically the same)

Implementation:

1. Recursive Implementation

1/** 2 * definition for Binary Tree 3 * struct treenode {4 * int val; 5 * treenode * left; 6 * treenode * right; 7 * treenode (int x ): val (x), left (null), right (null) {} 8 *}; 9 */10 class solution {11 public: 12 vector <int> levelorder (treenode * root) {13 vector <int> result; 14 traverse (root, 1, result); 15 return result; 16} 17 void traverse (treenode * root, size_t level, vector <int> & result) 18 {19 if (root = nullptr) return; 20 if (level> result. size () result. push_back (vector <int> (); 21 result [level-1]. push_back (root-> Val); 22 traverse (root-> left, level + 1, result); 23 traverse (root-> right, level + 1, result ); 24} 25 };

Ii. Iterative implementation
Iteration A Implementation 1
1/** 2 * definition for Binary Tree 3 * struct treenode {4 * int val; 5 * treenode * left; 6 * treenode * right; 7 * treenode (int x ): val (x), left (null), right (null) {} 8 *}; 9 */10/* Conventional sequence traversal ideas, each layer of the team ends into an empty node, as the flag */11 class solution {12 public: 13 vector <int> levelorder (treenode * root) {14 vector <int> vec_vec_tree; // create an empty vector and store the value of the last returned traversal binary tree. The value is 15 vector <int> level. // create an empty vector and store traversal 2 of each layer. The value of the Cross Tree is 16 treenode * P = root; 17 if (P = nullptr) return vec_vec_tree; // If the binary tree is empty, returns the empty vector <int> 18 queue <treenode *> queue_tree; // creates an empty queue 19 queue_tree.push (P ); // root node incoming queue 20 queue_tree.push (nullptr); // empty node incoming queue 21 While (! Queue_tree.empty () // until the queue is empty 22 {23 p = queue_tree.front (); // The value of the header node is 24 queue_tree.pop (); 25 if (P = nullptr &&! Level. empty () // The node is empty and the queue is not empty. 26 {27 queue_tree.push (nullptr); // enter the empty node, which is separated by 28 vec_vec_tree.push_back (level) from the next layer ); // The layer that has been traversed is queued for 29 levels. clear (); // clear vecor level30} 31 else if (P! = Nullptr) // If the node is not empty 32 {33 level. push_back (p-> Val); // traverse 34 if (p-> left) queue_tree.push (p-> left); // if there are left and right children, 35 if (p-> right) queue_tree.push (p-> right) in the queue; // note the queue sequence: First left, then right 36} 37 38} 39 return vec_vec_tree; 40} 41 };

B iteration implementation 2

1/** 2 * definition for Binary Tree 3 * struct treenode {4 * int val; 5 * treenode * left; 6 * treenode * right; 7 * treenode (int x ): val (x), left (null), right (null) {} 8 *}; 9 */10/* two queues implement */11 class solution {12 public: 13 vector <int> levelorder (treenode * root) {14 vector <int> result; 15 if (root = nullptr) return result; 16 queue <treenode *> current, next; // two queues, save the current and lower layers (Next) node 17 vector <int> level; // store the node value of each layer 18 current. Push (Root); 19 while (! Current. Empty () // until the binary tree traversal is completed 20 {21 While (! Current. empty () // until the binary tree traversal of the current layer is completed 22 {23 treenode * node = current. front (); // gets the first node of the queue, and the outgoing queue is 24 current. pop (); 25 level. push_back (node-> Val); // Access Node value 26 if (node-> left! = Nullptr) Next. Push (node-> left); // if there are left and right nodes, press 27 in the next queue if (node-> right! = Nullptr) Next. push (node-> right); // note that the queuing sequence is left and then right28} 29 result. push_back (level); // press the total vector30 level. clear (); // clear vector31 swap (next, current); // switch between the next queue and the current queue 32} 33 return result; 34} 35 };

 

Finally, we will explain how to implement the traversal from bottom to top and from right to left.

My idea: from bottom to top, we can directly reverse the result vector <int>.

From right to left, change the team order to the right and left.

 

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.