Leetcode: tree level order traversal II (sequence traversal of a binary tree 2)

Source: Internet
Author: User

Question:

Given a binary tree, returnBottom-up level orderTraversal of its nodes 'values. (ie, from left to right, level by level from leaf to root ).

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

    3   /   9  20    /     15   7

 

Return its bottom-up level order traversal:

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

 

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

Note:

1) similar to sequence traversal 1, The traversal direction of this question is different: from bottom up, the other is the same

2) You only need to add a line of statements output in reverse order to the code.

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 STD :: reverse (result. begin (), result. end (); // This line is 16 return result; 17} 18 void traverse (treenode * root, size_t level, vector <vector <int> & result) 19 {20 if (root = nullptr) return; 21 if (level> result. size () result. push_back (vector <int> (); 22 result [level-1]. push_back (root-> Val); 23 traverse (root-> left, level + 1, result); 24 traverse (root-> right, level + 1, result ); 25} 26 };

 

Ii. Iterative 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/* 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); // note the queue order: first left, right 36} 37 38} 39 STD :: reverse (vec_vec_tree.begin (), vec_vec_tree.end (); // This line is 40 return vec_vec_tree; 41} 42} more than 1 in sequence traversal };

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 STD: reverse (result. begin (), result. end (); // This line is 34 return result; 35} 36} more than 1 in sequence traversal };

 

 

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.