BFS solution is intuitive - here I will show a DFS based solution:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
int height;
vector<vector<int>> ret; int getHeight(TreeNode*p)
{
if(!p) return ;
return max(getHeight(p->left), getHeight(p->right)) + ;
}
void go(TreeNode *p, int level)
{
if(!p) return;
ret[height - - level].push_back(p->val);
go(p->left, level + );
go(p->right, level + );
}
public:
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
if(!root) return ret; height = getHeight(root);
ret.assign(height, vector<int>()); go(root, );
return ret;
}
};

LeetCode "Binary Tree Level Order Traversal II" using DFS的更多相关文章

  1. [LeetCode] 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]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  3. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. LeetCode——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 - Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  6. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  7. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

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

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

随机推荐

  1. cookie预:

    什么是cookie? cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie ...

  2. MyEclipse中Maven的配置

    之前在MyEclipse这个IDE中配置Maven,完成配置后启动Maven时出现-Dmaven.multiModuleProjectDirectory system propery is not s ...

  3. android 自定义view -- 实现自定义 邮箱验证的Edittext

    //onFinishInflate 当View中所有的子控件均被映射成xml后触发 /** * 实现自定义 实现邮箱验证的EidtText */public class CustomEditText ...

  4. css3简单介绍

    关于css3我先介绍几个简单的选择器: 先进行设置: 字符串匹配属性选择器: E[alt^="a"]  选择属性中以a开头的元素: E[alt$="a"]  选 ...

  5. java堆、栈、堆栈的区别

    1.栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势是,存取速度比堆要快,仅次于直接位于CP ...

  6. OD调试篇8

    那么今天需要破解的呢,是这样一款软件. 程序刚刚进去会发现一个nag弹窗   说没有注册,要花20美金才能注册.只有5天的限制期限可以用了 进去之后 点击help里的关于这款软件   也显示了这是一个 ...

  7. C#中的委托

    public delegate void SayHello(string name); class Program { static void Main(string[] args) { SayHel ...

  8. substring,substr,和slice的区别详解。

    1.Substring(x,y) : 输出一个字符串,当其中只有一个参数时,会输出从x开始到结尾的String. 举例: var str="hello";        conso ...

  9. leetcode52. N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  10. Maven生命周期详解

    来源:http://juvenshun.iteye.com/blog/213959 Maven强大的一个重要的原因是它有一个十分完善的生命周期模型(lifecycle),这个生命周期可以从两方面来理解 ...