【Binary Tree Level Order Traversal】cpp

题目:

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

代码:

/**
 * 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>> levelOrder(TreeNode* root) {
            vector<vector<int> > ret;
            if (!root) return ret;
            vector<int> tmp_ret;
            deque<TreeNode *> currLevel, nextLevel;
            currLevel.push_back(root);
            while ( !currLevel.empty() )
            {
                while ( !currLevel.empty() )
                {
                    TreeNode * tmp = currLevel.front();
                    currLevel.pop_front();
                    tmp_ret.push_back(tmp->val);
                    if ( tmp->left ) nextLevel.push_back(tmp->left);
                    if ( tmp->right ) nextLevel.push_back(tmp->right);
                }
                ret.push_back(tmp_ret);
                tmp_ret.clear();
                std::swap(currLevel, nextLevel);
            }
            return ret;
    }
};

tips:

核心:两个队列技巧

1. 采用两个队列,一个队列存放本层TreeNode,另一个队列存放下一层的TreeNode

2. 本层Node逐个出队的同时加入tmp_ret的vector,下一层的Node逐个入队

3. 本层Node全部出队之后,tmp_ret推入ret(并注意清空tmp_ret,第一次没有清空tmp_ret没有AC)

4. 逐个时候currLevel队列已经空了,nextLevel队列存放的都是下一层的节点,利用swap操作交换二者。(这个交换是O(1)的指针交换)

完毕。

======================================

一些关于二叉树 deque queue的参考资料:

http://www.cnblogs.com/way_testlife/archive/2010/10/07/1845264.html

http://stackoverflow.com/questions/2247982/deque-vs-queue-in-c

时间: 2024-12-16 04:58:44

【Binary Tree Level Order Traversal】cpp的相关文章

【Binary Tree Post order Traversal】cpp

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 代码: stack 1: /** * Definition for a binar

【Binary Tree Level Order Traversal II 】cpp

题目: 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 tr

【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

【Leetcode】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] ] 思路:使用

【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 return its bottom-up level order trave

【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 return its bottom-up level order traver

【leetcode】Binary Tree Level Order Traversal I &amp; II

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] ] 分层遍历二叉

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 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

【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】

[102-Binary Tree Level Order Traversal(二叉树层序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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