[LeetCode] Binary Tree Level Order Traversal 2
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example: Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7]
[9,20],
[3],
] head.h里代码实现如下:
#include<vector>
#include<queue>
using namespace std;
#define NULL 0 struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x),left(NULL),right(NULL) {}
}; struct Node
{
TreeNode *node;
int level;
Node(){}
Node(TreeNode *n,int l):node(n),level(l){}
}; class Solution
{
private:
vector<vector<int> > ret;
vector<vector<int> > retReverse;
public:
vector<vector<int> > leverOrder(TreeNode *root)
{
ret.clear(); if(root == NULL)
return ret; queue<Node> q;
q.push (Node(root,0)); vector<int> a;
int curLever = -1; while(!q.empty() )
{
Node node = q.front();
if(node.node->left)
q.push(Node(node.node->left,node.level+1));
if(node.node->right)
q.push(Node(node.node->right,node.level+1)); if(curLever != node.level)
{
if(curLever != -1)
ret.push_back(a);
curLever = node.level ;
a.clear();
a.push_back(node.node->val);
}
else
a.push_back(node.node->val); q.pop();
} ret.push_back(a); retReverse.clear(); for(int i=ret.size()-1;i>=0;i--)
retReverse.push_back(ret[i]); return retReverse; }
};
main.cpp里代码实现如下:
#include"head.h"
#include<iostream> int main()
{
TreeNode *root;
root=new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
Solution sol;
for(vector<vector<int> >::size_type ix=0;ix!=sol.leverOrder(root).size();++ix)
{for(vector<int>::size_type i=0;i!=sol.leverOrder(root)[ix].size();++i)
cout<<sol.leverOrder(root)[ix][i]<<" ";
cout<<endl;}
delete root->right->right;
delete root->right->left;
delete root->right;
delete root->left;
delete root;
return 0;
}
[LeetCode] Binary Tree Level Order Traversal 2的更多相关文章
- 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 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 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [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 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [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 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- 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
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- SPOJ371 Boxes(最小费用最大流)
把球当作水. #include<cstdio> #include<cstring> #include<queue> #include<algorithm> ...
- Android 滑动冲突处理
要想解决滑动冲突就必须好好理解 Android 的事件分发机制.不了解 Android 事件分发机制的请先参考资料学习一下. 一般有 2 种方法 1 外部拦截法 这个非常简单,因为事件是从父 view ...
- The number of divisors(约数) about Humble Numbers[HDU1492]
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- extjs tips
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 【转】Profiling application LLC cache misses under Linux using Perf Events
转自:http://ariasprado.name/2011/11/30/profiling-application-llc-cache-misses-under-linux-using-perf-e ...
- 【wikioi】1690 开关灯(线段树)
http://wikioi.com/problem/1690/ 这题可不能算是水题了.. 在线段树中,我只想到了lazy改变,但是没想到lazy变后size怎么变,我的策略变成了lazy为0时size ...
- 最近公共祖先(lca)
囧啊囧. lca的求法太多了 倍增,tarjan,st,lct,hld.... 后边三个我就不写了,其中st我没写过,估计用不上,在线用倍增,离线用tarjan就行了. 嗯. 第一种,倍增(O(nlo ...
- BZOJ3339 Rmq Problem
[bzoj3339]Rmq Problem Description Input Output Sample Input 7 5 0 2 1 0 1 3 2 1 3 2 3 1 4 3 6 2 7 Sa ...
- 分布式架构高可用架构篇_02_activemq高可用集群(zookeeper+leveldb)安装、配置、高可用测试
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- FZU 2092 bfs+记忆化搜索
晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...