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 ...
随机推荐
- ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...
- iOS奇怪的问题,键盘偏移异常
现象描述: 点击UITextView,键盘会弹出.然后点击添加图片,弹出了ActionSheet,键盘自动收缩.接着关闭ActionSheet,发现键盘又弹出了,接着点击Done,想要隐藏键盘,却发现 ...
- 图形化升级单机oracle 11.2.0.4 到 12.2.0.1
1. 讲补丁包上传到 Oracle server ,解压.安装 [oracle@11g tmp]$ unzip linuxx64_12201_database.zip 2. 检查当前版本 SQL> ...
- 如何恢复,迁移,添加, 删除 Voting Disks
如何恢复,迁移,添加, 删除 Voting Disks恢复流程 在11gR2 之前,我们可以直接直接使用dd命令对voting disk进行备份.DD示例 备份votedisk盘:[root@raw1 ...
- Marionettejs
Marionette是牵线木偶的意思,这个库是对Backbone的一次更高层次封装.这样的封装有两个目标: 减少重复的工作,提高使用Backbonejs时的生产效率给复杂应用页面提供更多的结构,以支撑 ...
- Maven(5)-优化和重构POM
本文主要介绍如何优化pom,杜绝重复(DRY). 1)模块重复依赖: 2)坐标版本号重复: 3)兄弟依赖 一.项目骨架 上图说明: multi-module-project是一个有多个模块构成的项目, ...
- Scala总结
Scala总结 ===概述 scala是一门以Java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程的最佳特性结合在一起的静态类型编程语言. scala是纯粹的面向对象的语言.java虽然是面 ...
- Java 的 Tuple 元组数据类型
元组类型,即 Tuple 常在脚本语言中出现,例如 Scala 的 ("Unmi", "china@qq.com", "blahbla"). ...
- css画三角形
效果图: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 杂项-权限管理:RBAC
ylbtech-杂项-权限管理:RBAC 基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中, ...