LeetCode:Binary Tree Level Order Traversal II

原创 2015年11月18日 14:37:43

Binary Tree Level Order Traversal II

Total Accepted: 60177 Total Submissions: 188407 Difficulty: Easy

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]
]
























code:

/**
 * 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) {
        vector<vector<int>> ret;
        if(NULL == root) return ret;
        queue<TreeNode *> q[2];  // 队列实现层次遍历,需要两个
        stack<vector<int>> s;  // 保存中间结果
        int cur=0;
        q[cur].push(root);
        while(!q[cur].empty()) {
            vector<int> tmp;
            while(!q[cur].empty()) {
                TreeNode *p = q[cur].front();
                tmp.push_back(p->val);
                if(p->left) q[cur^1].push(p->left);  // 交替入队
                if(p->right) q[cur^1].push(p->right);  
                q[cur].pop();
            }
            cur^=1;
            s.push(tmp);
        }
        while(!s.empty()) {
            ret.push_back(s.top());
            s.pop();
        }
        return ret;
    }
};


版权声明:本文为博主原创文章,转载请注明出处。

相关文章推荐

[LeetCode]Binary Tree Level Order Traversal II(JAVA)

这道题我真的是使出了洪荒之力啊 /** * Definition for a binary tree node. * public class TreeNode { * int...
  • jsh0123
  • jsh0123
  • 2016年09月14日 10:32
  • 163

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

Binary Tree Level Order Traversal II [LeetCode] Given a binary tree, return the bottom-up level orde...

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

leetcode 之Binary Tree Level Order Traversal I和II 解题思路

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from lef...

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

LeetCode 107. Binary Tree Level Order Traversal II

107. Binary Tree Level Order Traversal IIDescription Given a binary tree, return the bottom-up leve...

LeetCode(107)Binary Tree Level Order Traversal II

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left t...

LeetCode 107. Binary Tree Level Order Traversal II 树的BFS、DFS

Binary Tree Level Order Traversal II 题意 思路 代码 BFS DFS107. Binary Tree Level Order Traversal II Giv...

LeetCode 之 Binary Tree Level Order Traversal II

原题: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, ...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:Binary Tree Level Order Traversal II
举报原因:
原因补充:

(最多只允许输入30个字)