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 of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
/**
* 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 {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
vector<int> one_res; TreeNode* p = root;
TreeNode* first = NULL; queue<TreeNode*> que;
if(p) que.push(p); while(!que.empty()){
p = que.front();
que.pop(); if(first == p){//碰到每层的第一个时就把上一层次的所有结点加入结果集
res.push_back(one_res);
one_res.clear();
first = NULL;
} one_res.push_back(p->val); if(first==NULL && p->left!=NULL){
first = p->left;
}
if(first==NULL && p->right!=NULL){
first = p->right;
} if(p->left){
que.push(p->left);
}
if(p->right){
que.push(p->right);
}
} if(!one_res.empty()){
res.push_back(one_res);
}
return res;
}
};
Binary Tree Level Order Traversal II
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]
]
/**
* 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 {
private:
void levelOrderBottom(TreeNode* root,vector<vector<int>>& res,int depth){
if(!root) return;
if(depth==res.size()){
res.push_back({});
}
res[depth].push_back(root->val);
levelOrderBottom(root->left,res,depth+);
levelOrderBottom(root->right,res,depth+);
}
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
levelOrderBottom(root,res,);
reverse(res.begin(),res.end());
return res;
}
};
2.先求高度,无需反转,4ms
/**
* 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 {
private:
int getTreeHeith(TreeNode* root){
if(!root) return ;
return max(getTreeHeith(root->left) ,getTreeHeith(root->right)) + ;
}
void levelOrderBottom(TreeNode* root,vector<vector<int>>& res,int depth){
if(!root) return;
res[depth].push_back(root->val);
levelOrderBottom(root->left,res,depth-);
levelOrderBottom(root->right,res,depth-);
}
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
int dep = getTreeHeith(root);
vector<vector<int>> res(dep,vector<int>());
levelOrderBottom(root,res,dep-);
return res;
}
};
Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II的更多相关文章
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal
Title: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...
- HDU 3999 The order of a Tree
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu3999The order of a Tree (二叉平衡树(AVL))
Problem Description As we know,the shape of a binary search tree is greatly related to the order of ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
随机推荐
- PHP学习笔记十八【构造函数】
<?php class Person{ public $name; public $age; //定义构造函数 function 空格__construct 构造方法没有返回值,对象自动调用 p ...
- No1_2. 流程控制_java学习笔记
import java.util.Scanner; import java.lang.Math; public class HelloForWhile { /** * 文档注释,程序名称:HelloF ...
- vs2010中自动实现抽象方法
由于刚接触vs,感官上虽然和eclipse差不多,但是一些快捷都不太相同,导致一开始使用时候非常不习惯. 不过刚开始嘛,写点相当小白的东西,也没有用到太多功能,也就暂时忽视,用的时候再说. 但是今天, ...
- 清空DNS缓存
昨天写了个Python的脚本,功能就是爬取一个网页上的Google ip地址再写入到本机的hosts里面去. 但是写完并且运行完成之后发现上不了Google.于是想到了是不是要清空一下DNS的缓存.不 ...
- BNUOJ flower (搜索)
春天到了,师大的园丁们又开始忙碌起来了. 京师广场上有一块空地,边界围成了一个多边形,内部被划分成一格一格的.园丁们想在这个多边形内的每一格内种植一些花. 现在请你帮忙计算一下一共最多可以种多少花. ...
- window7下statsvn统计代码量
下载statsvn:http://www.statsvn.org/ 将下载后的statsvn.jar放到d:\svn目录下; 打开cmd窗口切换到需要统计代码的项目目录如:d:\project\web ...
- 并行编译加快VS C++项目的编译速度
最近编译的项目都比较大,话说自己的电脑配置还行,但编译所花的时间还是很长,遇到需要重新编译整个项目的时候真的有回宿舍睡一觉的冲动.昨天一不小心被我发现了一款软件Xoreax IncrediBuild ...
- android gridview布局,实现长按某一个,所有项都显示删除的图标
最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按gridview中的某一项显示删除图标,此时点击某项便可删除,再长按取消删除图标. gridview的布局文件如下: <?xm ...
- excel内容转成xml
简单记录下如何将excel中的一个表格内容转成xml格式的文件. excel菜单栏中的"开发工具"下有专门处理xml的模块,如下图. 如果你的excel中看不到"开发工具 ...
- bzoj1643 [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
Description 农夫约翰已经从他的牧场中取得了数不清块数的正方形草皮,草皮的边长总是整数(有时农夫约翰割草皮的刀法不合适,甚至切出了边长为0的正方形草皮),他已经把草皮放在了一个奶牛贝茜已经知 ...