码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Binary Tree Level Order Traversal

时间:2014-12-08 10:44:51      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:leetcode   二叉树   

【题目】

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


【代码】

/*********************************
*   日期:2014-12-08
*   作者:SJF0115
*   题号: Binary Tree Level Order Traversal
*   来源:https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <malloc.h>
#include <vector>
#include <queue>
using namespace std;

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<int> level;
        vector<vector<int> > levels;
        if(root == NULL){
            return levels;
        }
        //入队列
        queue<TreeNode*> queue;
        queue.push(root);
        //当前层节点个数
        int count = 1;
        //下一层节点个数
        int nextCount = 0;
        // 层次遍历
        while(!queue.empty()){
            //取队列头元素
            TreeNode *p = queue.front();
            //入队列
            level.push_back(p->val);
            queue.pop();
            count--;
            //左子树
            if(p->left != NULL){
                queue.push(p->left);
                // 下一层节点数加一
                nextCount++;
            }//if
            // 右子树
            if(p->right != NULL){
                queue.push(p->right);
                // 下一层节点数加一
                nextCount++;
            }//if
            // 当前层访问完毕
            if(count == 0){
                count = nextCount;
                nextCount = 0;
                levels.push_back(level);
                level.clear();
            }//if
        }//while
        return levels;
    }
};

//按先序序列创建二叉树
int CreateBTree(TreeNode* &T){
    char data;
    //按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
    cin>>data;
    if(data == '#'){
        T = NULL;
    }
    else{
        T = (TreeNode*)malloc(sizeof(TreeNode));
        //生成根结点
        T->val = data-'0';
        //构造左子树
        CreateBTree(T->left);
        //构造右子树
        CreateBTree(T->right);
    }
    return 0;
}

int main() {
    Solution solution;
    TreeNode* root(0);
    CreateBTree(root);
    vector<vector<int> > vecs = solution.levelOrder(root);
    for(int i = 0;i < vecs.size();i++){
        for(int j = 0;j < vecs[i].size();j++){
            cout<<vecs[i][j];
        }
        cout<<endl;
    }
}


[LeetCode]Binary Tree Level Order Traversal

标签:leetcode   二叉树   

原文地址:http://blog.csdn.net/sunnyyoona/article/details/41800671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!