leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历

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,null,null,15,7],

    3
   /   9  20
    /     15   7

return its level order traversal as:

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

解题思路:

采用双队列来处理。

用当前队列current来处理本层的所有节点,将本层信息记录在vector中。用next来记录下一层的节点信息。

当前队列处理后,将本层信息的vector存储到结果vector中。清空存储本层信息的vector。将current和next交换。然后重新处理current队列。

代码如下:

/**
 * 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>> result;
        queue<TreeNode *> current,next;
        vector<int> level;
        
        if(NULL == root)
            return result;
        current.push(root);
        
        while(current.size() > 0)
        {
            while(current.size() > 0)
            {
                TreeNode *p = current.front();
                current.pop();
                level.push_back(p->val);
                if(p->left)
                    next.push(p->left);
                if(p->right)
                    next.push(p->right);
            }
            result.push_back(level);
            level.clear();
            current.swap(next);
        }
        
        return result;
    }
};

2016-08-05 17:21:32

时间: 2024-12-25 07:51:21

leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历的相关文章

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 (二叉树的层序遍历)

题目: 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] ] co

leetcode-Binary Tree Level Order Traversal 二叉树层序遍历

#include<stdio.h> #include<queue> using namespace std; typedef struct BiTree { int val; struct BiTree *lchild; struct BiTree *rchild; }BiTree; void main() { BiTree *root; queue<BiTree *> q; BiTree *t; q.push(root); int size; //核心代码 while

LeetCode102 Binary Tree Level Order Traversal 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,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 分

LeetCode102 Binary Tree Level Order Traversal 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,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 解

Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List&lt;list&gt;

问题描述: 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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15

leetcode102 Binary Tree Level Order Traversal

1 """ 2 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). 3 For example: 4 Given binary tree [3,9,20,null,null,15,7], 5 3 6 / 7 9 20 8 / 9 15 7 10 return its level order tr

【LeetCode】Binary Tree Level Order Traversal(层序遍历)

Question 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

102 Binary Tree Level Order Traversal 二叉树的层次遍历

给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回其层次遍历结果为:[  [3],  [9,20],  [15,7]]详见:https://leetcode.com/problems/binary-tree-level-order-traversal/description/ /** * Definition for