Binary Tree Level Order Traversal II [LeetCode]
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7]
[9,20],
[3],
]
BFS solution:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int> > ret;
if(root == NULL)
return ret;
vector<TreeNode *> level;
level.push_back(root);
while(true) {
if(level.size() == )
break;
vector<int> nums;
vector<TreeNode *> tmp;
for(auto item : level) {
nums.push_back(item->val);
if(item->left != NULL)
tmp.push_back(item->left);
if(item->right != NULL)
tmp.push_back(item->right);
}
ret.insert(ret.begin(), nums);
level = tmp;
}
return ret;
}
DFS solution:
void getLevelNums(TreeNode *root, vector<vector<int> > &ret, int level) {
if(ret.size() < level + ){
vector<int> nums;
nums.push_back(root->val);
ret.insert(ret.begin(), nums);
}else if (ret.size() >= level + ) {
ret[ret.size() - level - ].push_back(root->val);
} if(root->left != NULL)
getLevelNums(root->left, ret, level + ); if(root->right != NULL)
getLevelNums(root->right, ret, level + ); }
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int> > ret;
if(root == NULL)
return ret;
getLevelNums(root, ret, );
return ret;
}
Binary Tree Level Order Traversal II [LeetCode]的更多相关文章
- Binary Tree Level Order Traversal II——LeetCode
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Binary Tree Level Order Traversal II --leetcode C++
考察点 广度优先遍历--层次遍历 STL内容器的用法 广度优先遍历的时候,首先应该想到的就是借助于队列.还需要在遍历下一层之前保存当前层节点的数量 代码很简单: class Solution { pu ...
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
随机推荐
- JAVA排序--[选择排序]
package com.array; public class Sort_Select { /** * 项目名称:选择排序 ; * 项目要求:用JAVA对数组进行排序,并运用选择排序算法; * 作者: ...
- Spring AOP和AspectJ支持
学了Spring之后发现我都不知道java为何物-- 在这一章中有好几节,讲的切面编程 第一节:在项目中启用Spring的AspectJ注解支持 第二节:用AspectJ注解声明aspect 第三节: ...
- C# 文件流相关操作
二进制转换成图片: MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(ms ...
- .net 常用Response.ContentType
来源:http://blog.csdn.net/navy235/article/details/5982319 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 ...
- HDU-4514 湫湫系列故事——设计风景线 手动扩栈
题意:给定一个无向图,首先判定是否成环,然后求一条最长链. 分析:成环用并查集,最长链就是个最简单的树形dp了. #include <cstdlib> #include <cstri ...
- nodejs学习笔记<六>文件处理
nodejs处理文件模块:fs —> var fs = require(‘fs’); 读取文件:readFileSync & readFile 读取文件路径为绝对: 读取结果需要to ...
- mysql 中文字段排序( 按拼音首字母排序) 的查询语句
在处理使用Mysql时,数据表采用utf8字符集,使用中发现中文不能直接按照拼音排序 如果数据表tbl的某字段name的字符编码是latin1_swedish_ci select * from `tb ...
- (三)stm32之串口通信DMA传输完成中断
一.DMA功能简介 首先唠叨一下DMA的基本概念,DMA的出现大大减轻了CPU的工作量.在硬件系统中,主要由CPU(内核).外设.内存(SRAM).总线等结构组成,数据经常要在内存和外设之间,外设和外 ...
- Android应用程序构成
一个Android应用程序一般是由以下4个组件构成的: 活动(Activity) 意图(Intent) 服务(Service) 内容提供器(Content Provider) 这4个组件是构成andr ...
- 【官方方法】xcode7免证书真机调试
[官方方法]xcode7免证书真机调试 步骤比较简单,我就简单总结一下. 1. 进入xcode,菜单栏选择xcode –> preferences (快捷键 command + ,)在Accou ...