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. 【C&C++】查看代码运行时间

    查看代码运行时间有助于更好地优化项目代码 1. Windows平台 windows平台下有两种方式,精度有所不同,都需要包含<windows.h>头文件 1) DWORD GetTickC ...

  2. Python:正则表达式(二)

    则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Python 语 ...

  3. html锚链接

    锚点(anchor):其实就是超链接的一种,一种特殊的超链接 普通的超链接,<a href="路径"></a> 是跳转到不同的页面 而锚点,<a hr ...

  4. HTML DOM clearTimeout() 方法

    转自:http://www.w3school.com.cn/jsref/met_win_cleartimeout.asp 定义和用法 clearTimeout() 方法可取消由 setTimeout( ...

  5. <正则吃饺子> :关于mybatis中使用的问题(一)

    在公司项目开始之前,根据springboot .mybatis.Swagger2 整合了一个demo,在测试时候,遇到的问题,简单记录.之前在使用mybatis时候,没有注意到这一点. 1.错误:Th ...

  6. 新版 Spring下载方法

    1.百度 Spring 打开官方网站   http://spring.io/ 2.======================================= 3.================= ...

  7. Java Numbers

    通常情况下,当我们与数字打交道,使用原始数据类型,如字节,如int,long,double等 例子: int i = 5000; float gpa = 13.65; byte mask = 0xaf ...

  8. USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>

    [英文原题] Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange ...

  9. mysql查询语句in和exists二者的区别和性能影响

    mysql中的in语句是把外表和内表作hash 连接,而exists语句是对外表作loop循环,每次loop循环再对内表进行查询.一直大家都认为exists比in语句的效率要高,这种说法其实是不准确的 ...

  10. OpenGL — GLFW — 颜色

    OpenGL - GLFW - 颜色 参考教程:https://learnopengl-cn.readthedocs.io/zh/latest/02%20Lighting/01%20Colors/ 既 ...