[LeetCode] 107 Binary Tree Level Order Traversal II (easy)
原题
层序遍历,从自底向上按层输出。 左→右→中
解法一 :
DFS,求出自顶向下的,最后返回时反转一下。
class Solution
{
public:
vector<vector<int>> res;
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
int level = 0;
dfs(root, level);
reverse(res.begin(), res.end());
return res;
}
void dfs(TreeNode *root, int level)
{
if (root == NULL)
return;
if (level == res.size())
res.push_back({});
dfs(root->left, level + 1);
dfs(root->right, level + 1);
res[level].push_back(root->val);
}
};
解法二:
BFS
class Solution
{
public:
vector<vector<int>> res;
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
if (root == NULL)
return res;
int level = 0;
queue<TreeNode *> que;
que.push(root);
while (!que.empty())
{
vector<int> cur;
int level = que.size();
for (int i = 0; i < level; i++)
{
TreeNode *temp = que.front();
que.pop();
cur.push_back(temp->val);
if (temp->left != NULL)
que.push(temp->left);
if (temp->right != NULL)
que.push(temp->right);
}
res.insert(res.begin(), cur);
}
return res;
}
};
[LeetCode] 107 Binary Tree Level Order Traversal II (easy)的更多相关文章
- Java for LeetCode 107 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 107. Binary Tree Level Order Traversal II (二叉树阶层顺序遍历之二)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- leetcode 107 Binary Tree Level Order Traversal II ----- java
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java [Leetcode 107]Binary Tree Level Order Traversal II
题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II
相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
随机推荐
- 轻量级 Material Design 前端框架 MDUI (纯html,css,与css框架跟react vue不冲突)
MDUI 是一个轻量级的 Material Design 前端框架,对照着 Material Design 文档进行开发,争取 1:1 实现 Material Design 中的组件. 多主题支持 M ...
- 以太网,IP,TCP,UDP数据包分析(此文言简意赅,一遍看不懂的话,耐心的看个10遍就懂了,感谢作者无私奉献)
1.ISO开放系统有以下几层: 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 2.TCP/IP 网络协议栈分为应用层(Application).传输层(Tra ...
- java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory 解决办法
解决办法:引入file upload 模块. 在POM文件中添加如下内容: <!-- file upload part --> <dependency> <groupId ...
- HTML连载14-文字属性补充&简写
一.字体属性(补充) 1.如果设置的字体不存在,那么系统会使用默认的字体来显示宋体. font-family:"瞎写的一个字体"; 2.如果设置的字体不存在,而我们又不想用默认的字 ...
- abp(net core)+easyui+efcore仓储系统——创建应用服务(五)
abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...
- Netty源码分析--Channel注册(中)(六)
接上一篇,我们继续看 不知道大家第一次看这段代码的时候有没有一脸懵逼,反正我是一脸懵,为什么这个if else 最终都是调用的register0方法,都是一样的. 其实这里就是为什么Netty是线程安 ...
- java源码解析之String类(三)
上一节我们主要讲了String类的一些不是很常用的方法,其中需要掌握的如下,我就不再赘述了 public int length() public boolean isEmpty() public by ...
- 【linux杂记】Ubuntu查看端口使用情况
转载地址: https://www.linuxidc.com/Linux/2016-01/127345.htm Ubuntu查看端口使用情况,使用netstat命令: 查看已经连接的服务端口(ESTA ...
- shell脚本开发基本规范
当你的才华还撑不起你的野心的时候,你就应该静下心来学习.当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练.问问自己,想要怎样的人生. 欢迎加入 基础架构自动化运维:598432640,大数据S ...
- C# 位运算及实例计算
前言: 平时在实际工作中很少用到这个,虽然都是一些比较基础的东西,但一旦遇到了,又不知所云.刚好最近接触了一些相关这方面的项目,所以也算是对 这些内容重新温习实践了一遍.所以这篇不仅作为个人备忘,也分 ...