lintcode-dfs实现二叉树的层序遍历
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实现二叉树的层序遍历的更多相关文章
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- 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, ...
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解
壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 剑指Offer21 二叉树的层序遍历
/************************************************************************* > File Name: 21_PrintT ...
随机推荐
- 51nod 1149 Pi的递推式 组合数
题目大意: \(F(x) = 1 (0 \leq x < 4)\) \(F(x) = F(x-1) + F(x-\pi) (4 \leq x)\) 给定\(n\),求\(F(n)\) 题解: 我 ...
- openfire存储中文字符乱码解决办法
转载于: Xmpp问题总结:处理Openfire 中文乱码问题(2) openfire是一个非常不错的IM服务器,而且是纯Java实现,具有多个平台的版本,他的数据存储可以采用多种数据库,如MySQL ...
- keepalived基本应用解析
原地址:http://blog.csdn.net/moqiang02/article/details/37921051 概念简单认知: Keepalived:它的诞生最初是为ipvs(一些服务,内核中 ...
- 时间倒计时 JS
<div id="keleyi">Christmas Countdown</div> <script type="text/javascri ...
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- spring容器扩展功能之一:spring加载ApplicationContext.xml的四种方式
容器加载Bean的常见两个类ApplicationContext和BeanFactory, 一.首先,看看spring中加载配置在xml中的Bean对象到容器 spring 中加载xml配置文件的方式 ...
- 不支持PowerShell 2.0版本(don't support PowerShell version 2.0. )
在“程序包管理器控制台”使用命令“update-database”会提示:The Entity Framework Core Package Manager Console Tools don't s ...
- 问题:webservice浏览后 无法输入参数;结果:调试Web Service时不能输入参数的解决办法
使用.NET 开发Web Service,有一个很方便的功能就是可以通过IE直接测试Web Service.当你的Web Service的参数都是元数据类型,那么只要你使用IE浏览Web Servic ...
- Mysql 数据库时间更新字段
关于时间更新: 创建时间: CURRENT_TIMESTAMP 更新时间: 勾选根据时间戳更新
- SQL 时间及字符串操作
都是一些很基础很常用的,在这里记录一下 获取年月日: year(时间) ---获取年,2014 month(时间) ----获取月,5 day(时间) -----获取天,6 如果月份或日期不足两位数, ...