【LeetCode】107 - Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal 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 as:

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

Solution 1: 普通层次遍历,借助queue

/**
 * 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>> levelOrderBottom(TreeNode* root) {      //runtime: 8ms
        vector<vector<int>> vec;
        if(!root)return vec;
        vector<int> v;

        queue<TreeNode*> q1,q2;
        q1.push(root);
        while(!q1.empty()){
            TreeNode* temp = q1.front();
            q1.pop();
            v.push_back(temp->val);
            if (temp->left)
                q2.push(temp->left);
            if (temp->right)
                q2.push(temp->right);

            if(q1.empty()){
                if (!v.empty())
                    vec.push_back(v);
                v.clear();
                swap(q1, q2);
            }
        }
        reverse(vec.begin(),vec.end());    //or vector<vector<int>> ret(vec.rbegin(),vec.rend());return ret;     return vec;   }}

Solution 2: 递归, 待续

时间: 2024-08-19 13:43:31

【LeetCode】107 - Binary Tree Level Order Traversal II的相关文章

【LeetCode】107. Binary Tree Level Order Traversal II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51508308 Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to rig

【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树

按层输出二叉树,广度优先. 3 / 9 20 / 15 7 [ [15,7], [9,20], [3] ] /** * 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

【LeetCode】102. Binary Tree Level Order Traversal

题目: 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 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 提示

【Lintcode】070.Binary Tree Level Order Traversal II

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). Example Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

LeetCode OJ 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal 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 traver

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7],     3  

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal 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 trave

(二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up level order

leetcode笔记:Binary Tree Level Order Traversal II

一. 题目描述 Given a binary tree, return the bottom-up level order traversal 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 二. 题目分析 由于使用了vector,这一题只需Binar