题目描述:

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],
]

解题思路:

1)我的思路:(操作过于复杂)

 先将二叉树镜像
3
/ \
9 20
/ \
15 7
从下到上层序: 15 7 | 9 20 | 3 正常的层序:3 | 3 20 | 15 7
3
/ \
20 9
/ \
7 15
从上到下: 3 | 20 9 | 7 15 与原树从下到上层序(要求解的结果)正好是倒叙,可以将结果存入栈中。
使用另一个栈记录每层的节点数。
从两个栈中,读取最后结果  
class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int> > result;
if(root==nullptr)
return result;
//将二叉树镜像
MirrorBinaryTree(root);
//vector<vector<int> > res;
//return res; //层序遍历从左到右,从上到下,存入栈中
stack<int> treeData,numLevel;
queue<TreeNode *> pNode;
pNode.push(root);
TreeNode * curr;
int currCount=1,nextCount=0;
numLevel.push(currCount); //存第一个root节点,该变量用于存储每层的节点数 while(!pNode.empty()){ //队列不为空的时候 不能直接写 while(pNode)
curr = pNode.front();
pNode.pop();
treeData.push(curr->val); //存入当前节点值
currCount--;
if(curr->left){
pNode.push(curr->left);
nextCount++;
} if(curr->right){
pNode.push(curr->right);
nextCount++;
}
if(currCount==0){
if(nextCount!=0)
numLevel.push(nextCount); currCount = nextCount;
nextCount = 0; } }
//从栈中读取结果
vector<int> row;
//int index = 0;
while(!numLevel.empty()){ //error:while(numLevel)
int num = numLevel.top();
numLevel.pop();
while(num){
num--;
row.push_back(treeData.top());
treeData.pop();
}
//index++;
result.push_back(row);
vector<int> ().swap(row);//清空row中的值
} return result;
} void MirrorBinaryTree(TreeNode *root){ //返回值为空,在原树上修改,不要增加新的空间。
//终止条件
if(root==nullptr)
return ;
if((root->left==nullptr) && (root->right==nullptr))
return ;
//交换左右子树
TreeNode *temp = root->left;
root->left =root->right;
root->right = temp; if(root->left!=nullptr) //递归交换左子树
MirrorBinaryTree(root->left);
if(root->right!=nullptr) //递归交换右子树
MirrorBinaryTree(root->right);
}
};

2)广度优先遍历,然后对结果二维数组result的第一个维度做逆转

reverse(res.begin(),res.end());  //逆转的复杂度是不是很大?

class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root)
{
vector<vector<int>> res;
if(root==nullptr)
return res;
queue<TreeNode *> q;
q.push(root);
while(q.size()>0)
{
vector<int> level;
for(int i=0,n=q.size();i<n;i++)
{
TreeNode *temp = q.front();
q.pop();
level.push_back(temp->val);
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
res.push_back(level);
}
reverse(res.begin(),res.end()); //c++ 使用自带函数
return res;
}
};

3) DFS 深度优先遍历  

 思路很简便:初始化二维数组,存取数字时,从二维数组的第一维度的最大值存储(即从最后一行开始存,然后存倒数第二行)

初始化时,要知道二维数组一共有多少行,求树的高度即可。

class Solution {
public:
int getHeight(TreeNode *root)
{
if(!root) return 0;
return max(getHeight(root->left),getHeight(root->right))+1;
}
vector<vector<int> > levelOrderBottom(TreeNode *root)
{
if(!root) return vector<vector<int>>();
vector<vector<int>> res(getHeight(root),vector<int>()); //初始化二维数组
dfs(root,res.size()-1,res);
return res;
}
void dfs(TreeNode *root,int height,vector<vector<int>> &res) //定义时取应用,避免复制&res
{
if(!root)
return;
res[height].push_back(root->val);
dfs(root->left,height-1,res);
dfs(root->right,height-1,res);
}
}; 

4) 思路:用递归实现层序遍历

与正常遍历不同的是,先进行下一层递归,再把当前层的结果保存到res中  

//实现1 res定义为私有变量
class Solution {
public: vector<vector<int> > levelOrderBottom(TreeNode *root)
{
// vector<vector<int>> res;
if(!root) return res; queue<TreeNode *> currQueue;
currQueue.push(root);
levelOrderBottom(currQueue);
return res;
}
void levelOrderBottom(queue<TreeNode *> currQueue){
if(currQueue.empty())
return; int numLevel = currQueue.size(); //层数
vector<int> row;
//读取一层
for(int i=0;i<numLevel;i++){
TreeNode * pNode = currQueue.front();
currQueue.pop();
if(pNode->left)
currQueue.push(pNode->left);
if(pNode->right)
currQueue.push(pNode->right); row.push_back(pNode->val);
} levelOrderBottom(currQueue);
//先递归后存储,递归到最后一行时,才会开始存储。因此会先存最后一行,满足题目倒叙要求
res.push_back(row); }
private:
vector<vector<int>> res;
};
//实现2  res在函数内定义,并传入引用。避免复制引起的操作
class Solution {
public: vector<vector<int> > levelOrderBottom(TreeNode *root)
{
vector<vector<int>> res;
if(!root) return res; queue<TreeNode *> currQueue;
currQueue.push(root);
levelOrderBottom(currQueue,res);
return res;
}
void levelOrderBottom(queue<TreeNode *> currQueue,vector<vector<int>>& res){
if(currQueue.empty())
return; int numLevel = currQueue.size(); //层数
vector<int> row;
//读取一层
for(int i=0;i<numLevel;i++){
TreeNode * pNode = currQueue.front();
currQueue.pop();
if(pNode->left)
currQueue.push(pNode->left);
if(pNode->right)
currQueue.push(pNode->right); row.push_back(pNode->val);
} levelOrderBottom(currQueue,res);
//先递归后存储,递归到最后一行时,才会开始存储。因此会先存最后一行,满足题目倒叙要求
res.push_back(row); }
};

  

  

  

 

  

  

N3-2 - 树 - binary-tree-level-order-traversal-ii的更多相关文章

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

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

  3. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  4. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

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

  6. LeetCode_107. Binary Tree Level Order Traversal II

    107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...

  7. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

  8. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

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

  10. 【Binary Tree Level Order Traversal II 】cpp

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

随机推荐

  1. Spring MVC-静态页面示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_static_pages.htm 说明:示例基于Spring MVC 4.1.6. ...

  2. [Oracle]行列转换(行合并与拆分)

    使用wmsys.wm_concat 实现行合并 在 Oracle  中, 将某一个栏位的多行数据转换成使用逗号风格的一行显示.能够使用函数  wmsys.wm_concat 达成. 这个在上一篇 or ...

  3. swift 声明特性 类型特性

    原文地址:http://www.cocoachina.com/newbie/basic/2014/0612/8801.html 特性提供了关于声明和类型的很多其它信息.在Swift中有两类特性,用于修 ...

  4. 开源 免费 java CMS - FreeCMS2.0 会员password设置

    项目地址:http://www.freeteam.cn/ password设置 从右側管理菜单点击password设置进入.   输入正确的当前password和新password后点击改动就可以.

  5. 聚类k-means/k-means++/fcm学习笔记

    聚类主要是一种无监督学习.用来发现未标注数据的隐藏结构,主要是用来给数据分组.聚类算法一般有硬聚类(k-means,k-means++)和软聚类FCM(fuzzy c-means).聚类眼下广泛应用于 ...

  6. 【java项目实战】ThreadLocal封装Connection,实现同一线程共享资源

    线程安全一直是程序员们关注的焦点.多线程也一直是比較让人头疼的话题,想必大家以前也遇到过各种各种的问题.我就不再累述了.当然,解决方案也有非常多,这篇博文给大家提供一种非常好的解决线程安全问题的思路. ...

  7. Swift-理解值类型

    在这里,我们要讲讲值类型和写时复制.在 swift 的标准库中,所有的集合类型都使用了写时复制.我们在本篇文章中看一下写时复制如何工作的,并且如何实现它. 引用类型 使用 swift 的 Data 和 ...

  8. 二重积分的计算 —— 交换积分顺序(exchange the order of integration)

    交换积分顺序的诀窍在数形结合: 1. 几句顺口溜 后积先定限,限内穿条线,先交下限写,后交上限见 先积 x,画横线(平行于 x 轴),右减左: 先积 y,画竖线(平行于 y 轴),上减下: 2. 简单 ...

  9. Hadoop MapReduce编程 API入门系列之wordcount版本5(九)

    这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount1; import java.io.IOException; i ...

  10. Function 构造器及其对象、方法

    一.基础 Function 是一个构造器,能创建Function对象,即JavaScript中每个函数实际上都是Function 对象. 构造方法:  new Function ([arg1[, ar ...