LeetCode "Binary Tree Level Order Traversal II" using DFS
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的更多相关文章
- [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 ...
- [leetcode]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- [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 ...
- 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 ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- 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 ...
随机推荐
- 用c语言编写二分查找法
二分法的适用范围为有序数列,这方面很有局限性. #include<stdio.h> //二分查找法 void binary_search(int a[],int start,int mid ...
- css透明度的兼容!!!
以前总是写透明度的代码,今天弄了个弹出框要求就边框透明,于是有了下边的结论,虽然很多东西不合理了,日后找到更合理在做更新!!! html <div class="new_playerf ...
- HDU 1240 Asteroids! 解题报告
//这道题做完我只有 三个感受 第一:坑: 第二 : 坑! 第三:还是坑! 咳咳 言归正传 WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解 : 类似胜利大逃亡 只 ...
- CodeForces #367 div2 D Trie
题目链接:Vasiliy's Multiset 题意:这里有一个set容器,有三种操作,+ num, - num, ? num,分别代表往容器里加上num,或者拿走num,或着从容器里找一个数temp ...
- 自己常用的8个Web在线工具
为什么要用 Web 在线工具呢?有两个原因,第一,它不受限于物理平台,我既可以在自己的电脑上使用,也可以在公司或亲戚朋友的电脑上使用(不管对方的操作系统是什么,只要能上网):第二,可以解放硬盘,减少 ...
- 记录一次fat32格式U盘不识别问题
升级了4.1.15内核发现U盘不识别了,考虑到内核编译前的配置是通过localmodconfig完成的,所以大略是缺模块导致. 于是开始查配置,USB控制器,EHCI,mass storeage de ...
- ASP.NET Cookie存值问题
想必 用Cookie存值已经是很普遍的了,我也是刚学习了一点皮毛,现在就记下一点知识,便于日后翻阅. 1.C#代码存取Cookie值 //用Request获取到客户端Cookie 判断是否为空 if ...
- leetcode36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- Flex Builder读书笔记(二)——MXML
MXML类似于HTML,它提供各种标签来定义用户界面,但是它比HTML的结构更为严格,并拥有跟多的应用标签.MXML不仅包括可视标签,还包括不可视标签,如web service连接.数据绑定和动画效果 ...
- c++对象成员的引用---12
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 在程序中经常需要访问对象中的成员.访问对象中的成员可以有3种方法: 通过对象名和成员运算符访问对象中的成 ...