(Leetcode) Binary Tree level Order traversal

Source: Internet
Author: User

Binary Tree level Order Traversalmy SubmissionsQuestionSolutionTotal Accepted £ º 67360 Total submissions: 228717 Difficulty: Easy

Given a binary tree, return the level order Traversal of its nodes ' values. (ie, from left-to-right, level by level).

For example:
Given binary tree `{3,9,20,#,#,15,7}` ,

`    3   /   9    /   7`

Return its level order traversal as:

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

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

`1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8  * };9  */Ten classSolution { One  Public: Avector<vector<int>> Levelorder (treenode*root) { -Queue<treenode*>que; -vector<vector<int>>ret; the         if(!root)returnret; -         intCur,next; - Que.push (root); -Cur =1; next =0; +vector<int>tmp; -          while(!que.empty ()) +         { Atreenode*Tmpnode; atTmpnode =Que.front (); -Tmp.push_back (tmpnode->val); - Que.pop (); -cur--; -             if(Tmpnode->left) {Que.push (tmpnode->left); next++; } -             if(Tmpnode->right) {Que.push (tmpnode->right); next++; } in             if(cur = =0{cur = next; next =0; Ret.push_back (TMP); Tmp.clear ();} -              to         } +          -         returnret; the     } *};`

(Leetcode) Binary Tree level Order traversal

Related Keywords:

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.