# Binary Tree level Order traversal II

Source: Internet
Author: User

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode (int x): Val (x), left (null), right (null) {}
* };
*/
Class Solution {
Public
vector<vector<int> > result;
void Solve (treenode*root,int dep)
{
if (root==null)
return;
if (Dep==result.size ())//There is no element in the current result, it is the structure of a two-layer vector
{vector<int>temp;
Result.push_back (temp); So you have to push a vector container in. If it is on the same level as the previous node, only the first node is added to the vector container, followed by a direct add
}
Result[dep].push_back (root->val);//Add elements to the current container
if (root->left!=null)
Solve (root->left,dep+1);
if (root->right!=null)
Solve (root->right,dep+1);
}

Vector<vector<int>> levelorderbottom (treenode* root) {
Solve (root,0);
Reverse (Result.begin (), Result.end ());
return result;
Return vector<vector<int>> (Result.rbegin (), Result.rend ());
}
};

Binary Tree level Order traversal II

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.