题目:Binay 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 {,,,#,#,,},

如下一棵树

转换之后需要输出这样的形式:

如下,见代码:

 struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode():val(),left(NULL),right(NULL) {}
TreeNode(int x): val(x), left(NULL),right(NULL) {}
}; vector<vector<int> > levelOrderTraversal(TreeNode *root) //非递归的中序遍历(用栈实现)
{ queue<TreeNode *> tree_queue;
vector<vector<int> > tree_vector;
vector<int> svector; if (NULL == root) {
return tree_vector;
}
TreeNode *pTemp = root;
tree_queue.push(root);
tree_queue.push(NULL); //the end of one level. while (true) {
TreeNode *pTemp = tree_queue.front();
tree_queue.pop(); if (!pTemp) { //get the null, put vector<> to vector<vector<>>
tree_vector.push_back(svector);
svector.clear();
if (tree_queue.empty())
break;
tree_queue.push(NULL);
}
else {
svector.push_back(pTemp->val);
if (pTemp->left)
tree_queue.push(pTemp->left);
if (pTemp->right)
tree_queue.push(pTemp->right);
}
}
return tree_vector;
}

LeetCode: 102_Binary Tree Level Order Traversal | 二叉树自顶向下的层次遍历 | Easy的更多相关文章

  1. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. [Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [Leetcode] Binary tree level order traversal二叉树层次遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  5. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

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

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

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

  9. [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. 微信小程序---分包加载(subpackages)及报错

    某些情况下,开发者需要将小程序划分成不同的子包,在构建时打包成不同的分包,用户在使用时按需进行加载. 分包很简单,具体步骤如下: 在app.json中,创建subPackages,root表示分包A的 ...

  2. Unity中的点击,长按,划动

    public void GetClickType() { if(Input.GetMouseButtonDown(0)) { if(isGetBeginPos) { beginPosition = I ...

  3. HTML中的Meta标签详解

    emta标签的组成:meta标签分两大部分:HTTP-EQUIV和NAME变量. HTTP-EQUIV:HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正确和精确地显 ...

  4. android的事件分发传递机制

    事件的分发与传递最重要的三个处理方法是 dispatchTouchEvent onInterceptTouchEvent onTouchEvent 综合来说事件的 传递是由外层向里层传递,而处理是从里 ...

  5. MVC的SignalR例子

    # SignalR学习 ASP.NET SignalR 是为.NET 开发者提供即时通讯Web 应用的类库.即时通讯Web服务就是服务器将内容自动推送到已经连接的客户端,而不是服务器等待客户端发起一个 ...

  6. Solidity合约间的调用-1

    当调用其它合约的函数时,可以通过选项.value(),和.gas()来分别指定,要发送的ether量(以wei为单位),和gas值. pragma solidity ^; contract InfoF ...

  7. 带标签的循环语句、switch

    今天继续更新,控制流程的剩余部分内容,带标签的循环语句中的continue/break 的使用方法,以及switch关键字的使用方法.例1:带标签的continue/break.package com ...

  8. CentOS 7 查询yum安装的软件及路径

    来源:CentOS 7 查询yum安装的软件及路径 先执行下面的命令,查看所有的已安装软件名称. rpm -qa 然后执行 rpm -ql 软件名称 就可以显示软件的安装路径.   [root@loc ...

  9. java ssh执行shell脚本

    1.添加依赖 com.jcraft:jsch ch.ethz.ganymed:ganymed-ssh2:262 2.获取连接 conn = new Connection(ip, port); conn ...

  10. ORM初识和数据库操作

    ORM简介 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使用描述对象和数据库之 ...