Leetcode[tree]: Binary Tree level Order traversal II

Source: Internet
Author: User

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

This simple problem can be solved: using Leetcode[tree]: Binary Tree level order traversal the same method to get top-down sequence traversal results, the result is flipped. It can also be inserted without plugging from the tail and inserting from the head.

My C + + code is implemented as follows:

vector<vector<int> > Levelorderbottom (TreeNode *root) {    vector<vector<int> > result;    Vector<int> level;    Queue<treenode *> LEVELQ;    if (root) levelq.push (root);            while (!levelq.empty ()) {        level.clear ();        int size = Levelq.size ();        for (int i = 0; i < size; ++i) {            TreeNode *node = Levelq.front ();            Levelq.pop ();            Level.push_back (node->val);            if (node->left) Levelq.push (node->left);            if (node->right) Levelq.push (node->right);        }        Result.insert (Result.begin (), level);    }    return result;}


Leetcode[tree]: Binary Tree level Order traversal II

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.