class Solution {
/**
* @param root: The root of binary tree.
* @return: Level order a list of lists of integer
*/
struct node{
TreeNode* tn;
int cnt;
node(TreeNode* tn, int cnt){
this->tn = tn;
this->cnt = cnt;
}
}; public:
void dfs(vector<vector<int>> &ans, queue<node*> &que){
if(que.empty()) return ;
node* now = que.front();
que.pop(); if(now->tn->left) que.push(new node(now->tn->left, now->cnt + ));
if(now->tn->right) que.push(new node(now->tn->right, now->cnt + )); if((int)ans.size() - < now->cnt){
vector<int> v(,now->tn->val);
ans.push_back(v); } else {
ans[now->cnt].push_back(now->tn->val);
} dfs(ans, que); }
vector<vector<int>> levelOrder(TreeNode *root) {
// write your code here
vector<vector<int>> ans;
if(root == NULL) return ans;
queue<node*> que;
que.push(new node(root,));
dfs(ans, que);
return ans;
}
};

lintcode-dfs实现二叉树的层序遍历的更多相关文章

  1. 二叉树的层序遍历 BFS

    二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...

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

  3. 剑指offer 二叉树的层序遍历

    剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...

  4. leetcode之二叉树的层序遍历

    1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...

  5. LeetCode 102. 二叉树的层序遍历 | Python

    102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...

  6. 刷题-力扣-107. 二叉树的层序遍历 II

    107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...

  7. 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解

    壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...

  8. leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

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

  9. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

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

  10. 剑指Offer21 二叉树的层序遍历

    /************************************************************************* > File Name: 21_PrintT ...

随机推荐

  1. 白话算法(6) 散列表(Hash Table) 从理论到实用(下)

    [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还是谈一场恋爱,都差不多要经历这么4个阶段: 1)从零开始.没有束缚的轻松感.似 ...

  2. #define与typedef区别

    1) #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错.例如: #define PI 3.141 ...

  3. 三种 Failover 之 Client-Side Connect time Failover、Client-Side TAF、Service-Side TAF

    三种 Failover 之 Client-Side Connect time Failover.Client-Side TAF.Service-Side TAF 理论背景 Oracle  RAC 同时 ...

  4. POJ百练—IP地址转换

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; ]; void ...

  5. ceph-deploy mon add 失败

    ceph-deploy mon add 失败 标签(空格分隔): ceph-deploy 运维 问题描述: 现有集群只有一个mon,需要通过ceph-deploy mon add添加两个mon.在ad ...

  6. LAMP 2.0Apache日志切割

    每次访问网站就会产生若干条日志,当然前提是已经配置了日志. 配置日志的文件在 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 把注释掉的这两行打 ...

  7. 阶段3-团队合作\项目-网络安全传输系统\sprint2-线程池技术优化

    之前问题的存在,之前只是用一个客户端在与服务器进行连接,当多个客户端进行连接的时候会连接不上处于等待状态,说明以前我们的服务器只能同时处理一个请求,故需要修改 服务器: 单发:初始化--等待客户端连接 ...

  8. IDEA拷贝git上的最新项目资源

    File->new ->project version control->git-> 进入项目git对应的网址,选择第一个backstop,复制url: 输入git用户名和密码 ...

  9. 开源库ActiveAndroid + gson使用

    ActiceAndroid的简介 ActiveAndroid是一个活跃的记录风格的ORM(对象关系映射)库.ActiveAndroid可以让您保存和检索的SQLite数据库记录而没有写一个SQL语句. ...

  10. JpetStore目录文件关系分析

    http://www.cnblogs.com/sunsonbaby/archive/2004/09/11/42245.html 从功能方向  至上向下分析 com.ibatis.jpetstore.s ...