Leetcode_107_binary Tree level Order traversal II

Source: Internet
Author: User

Welcome to read the reference, if there are errors or questions, please leave a message to correct, thank you

107 Binary Tree level Order traversal II

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).

For example:
Given binary Tree {3,9,20,#,#,15,7},
3
/ \
9 20
/  \
15 7
Return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]


/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * Tre Enode (int x): Val (x), left (null), right (NULL) {} *};  *///Method One: bfsstruct node{TreeNode *node;  int level; Node (TreeNode *n, int L): node (n), level (L) {}};class solution {vector<vector<int>> ANS;PUBLIC:VECTOR&L        t;vector<int> > Levelorderbottom (TreeNode *root) {ans.clear ();                if (root = NULL) return ans;        Vector<int> temp;                int curlevel =-1;        Queue<node> Q;                Q.push (Node (root, 1));            while (!q.empty ()) {Node Nodeq = Q.front ();                        Q.pop ();            if (nodeq.node->left) Q.push (node (nodeq.node->left, nodeq.level+1));                        if (nodeq.node->right) Q.push (node (nodeq.node->right, nodeq.level+1));    if (curlevel! = nodeq.level) {            if (curlevel! =-1) ans.push_back (temp);                Temp.clear ();                Temp.push_back (Nodeq.node->val);            Curlevel = Nodeq.level;        } else Temp.push_back (Nodeq.node->val);                } ans.push_back (temp);        Reverse (Ans.begin (), Ans.end ());    return ans; }};


Method Two: Dfsclass solution {vector<vector<int>> ans;public:    void Levelorderutil (TreeNode *root, int Depth)    {        if (root==null)            return;        if (Ans.size () > Depth)            ans[depth].push_back (root->val);        else        {            vector<int> A;            A.push_back (root->val);            Ans.push_back (a);        }        Levelorderutil (Root->left, depth+1);        Levelorderutil (Root->right, depth+1);    }        vector<vector<int> > Levelorderbottom (TreeNode *root) {        ans.clear ();        Levelorderutil (root, 0);        Reverse (Ans.begin (), Ans.end ());        return ans;    };


Leetcode_107_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.