原题

层序遍历,从自底向上按层输出。 左→右→中

解法一 :

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)的更多相关文章

  1. 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 ...

  2. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  3. [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 ...

  4. (二叉树 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

随机推荐

  1. QT5---应用程序发布(使用windeployqt和NSIS)

      采用动态编译的方式发布程序,即release版本. 找齐动态依赖库(.dll) 方法一   用Dependency Walker这个工具去找少了那些dll,不过这个工具也不怎么靠谱,一个比较靠谱但 ...

  2. vs2010添加TSTCON( ActiveX Control Test Container )工具

    vs2010中的TSTCON( ActiveX Control Test Container )工具非自动安装,而是作为一个例程提供.所以应找到该例程,并编译: 如vs2010安装在默认路径则 1, ...

  3. hadoop之hive基本操作

    -- 清空表中的数据,保留表结构 truncate table tmp_userid; '); -- 搜索库或表支持正则表达式 show tables 'sa*'; show tables in ba ...

  4. You can't specify target table 'tbl_students' for update in FROM clause错误

    此问题只出现在mysql中 oracle中无此问题 在同一语句中,当你在select某表的数据后,不能update这个表,如: DELETE FROM tbl_students WHERE id NO ...

  5. Ubuntu --- lamp环境下安装php扩展和开启apache重写

    安装教程参考:http://www.laozuo.org/8303.html 1.安装php扩展(比如安装mbstring) 先搜索相关的包 apt-cache search php7 再安装 apt ...

  6. ZooKeeper 系列(三)—— Zookeeper常用 Shell 命令

    一.节点增删改查         1.1 启动服务和连接服务         1.2 help命令         1.3 查看节点列表         1.4 新增节点         1.5 查看 ...

  7. HBase 学习之路(六)——HBase Java API 的基本使用

    一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...

  8. swoole异步任务数据报表生成

    <?php include 'vendor/autoload.php'; class server { private $serv; private $db; /** * [__construc ...

  9. javascript匿名函数自调用

    // 匿名函数的自调用 /*var f1 = function() { console.log('我是一个匿名函数!'); }*/ // f1(); // 上面是定义一个匿名函数,然后调用,其实上面就 ...

  10. 什么是JS跨域请求

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...