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. 电脑的f5刷新不了

    新买的电脑,f5刷新不了页面,网上查了后发现是fn功能键的原因.同时fn+f5即可刷新.可是依然感觉好别扭... 按下fn+esc,再只按f5,就可以刷新页面了.神奇...

  2. class && struct

    http://blog.csdn.net/yuliu0552/article/details/6717915 struct可以包含成员函数,可以继承,可以实现多态. struct为数据结构,class ...

  3. JVM实用参数——新生代垃圾回收

    JVM实用参数目录 JVM实用参数——新生代垃圾回收 概述 第1部分  新生代垃圾回收介绍 第2部分 参数介绍 参考 第1部分  新生代垃圾回收介绍 本部分,我们将关注堆(heap) 中一个主要区域, ...

  4. C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)

    C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...

  5. 全真模拟 (1) day1

    第一题: 题目大意: 给出N个数的m对关系(a,b)表示a大于b. 每个数至少为100,求这些书最小可能的和. 解题过程: 1.看到这题就想到之前USACO的一道题,那题是N头牛排序,然后给出m对关系 ...

  6. ASP.NET关于WebPages的一点总结

    相比于早期的ASP,WebPage貌似只是多了一些Razor语法可以直接调用服务器代码,其他的内容HTML.样式CSS以及脚本JavaScript基本都是一样的处理方式 只是说内容HTML里面加入了更 ...

  7. Ionic 2.0.0-rc.1 发布,HTML5 移动应用框架

    Ionic 2.0.0-rc.1 发布了,Ionic Framework 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应用前端框架.本次更新内容如下: Bug 修 ...

  8. 获取su后执行的脚本的返回值

    错误的方式: # su - testuser -c "/tmp/test.sh; echo $?"Sun Microsystems Inc.   SunOS 5.10      G ...

  9. hdu 2063

    ps:二分匹配法,匈牙利算法.感觉自己只是学了点皮毛...这里贴上大神的微博,深入浅出的讲了匈牙利算法: http://blog.csdn.net/dark_scope/article/details ...

  10. PHP性能分析 - ngnx日志分析

    最终结果展示图: 图解:响应时间在40ms以内的请求数占请求总量的7%,40到80ms的的请求数占32.9%,依次类推... 性能问题有很多种可能,普通的情况通过xhprof可查得主要的性能损耗.但有 ...