leetcode_107题——Binary Tree Level Order Traversal II (二叉树,广度优先搜索,队列,栈)

Binary Tree Level Order Traversal II

Total Accepted: 37829 Total Submissions: 122499My Submissions

Question Solution

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

Hide Tags

Tree Breadth-first Search

Have you met this question in a real interview?

Yes

No

Discuss

#include<iostream>
#include<vector>
#include<list>
#include <stack>
using namespace std;

//Definition for binary tree
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

/*采用广度优先搜索的方法,一层一层的去搜索,搜索时用到了队列,将每一层的结点进队列,然后将每一层
的一次输出,而在题目要求中反过来显示,所以又增加了一个堆栈,来为后面反过来显示节点数据*/
vector<vector<int> > levelOrder(TreeNode *root) {
	vector<vector<int> > last_result;//最终的结果
	stack<vector<int> >  stack_result;//作为中间结果
	if(root==NULL)
		return last_result;

	list<TreeNode*> temp;//做计算的队列
	TreeNode* temp_node;//作为中间变量
	//int depth=1;
	int row_size=1;//记录每一层的结点个数
	temp.push_back(root);

	while(!temp.empty())
	{
		vector<int> temp_result;//设置一个装每一层结点的中间vector
		while(row_size--)
		{
			temp_node=temp.front();
			temp.pop_front();
			temp_result.push_back(temp_node->val);
			if(temp_node->left!=NULL)//将左子树进队列
				temp.push_back(temp_node->left);
			if(temp_node->right!=NULL)//将右子树进队列
				temp.push_back(temp_node->right);
		}
		row_size=temp.size();
		stack_result.push(temp_result);//将每一层的数据压栈
	}
	while(!stack_result.empty())//最后按栈中的元素再反向的输出
	{
		last_result.push_back(stack_result.top());
		stack_result.pop();
	}
	return last_result;
}
int main()
{

}

  

时间: 2024-10-10 12:24:56

leetcode_107题——Binary Tree Level Order Traversal II (二叉树,广度优先搜索,队列,栈)的相关文章

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. 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,null,null,15,7],     3  

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 traver

leetcode_102题——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] ] confus

lintcode 中等题:binary tree level order traversal ii 二叉树的层次遍历II

题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 按照从下往上的层次遍历为: [ [15,7], [9,20], [3] ] 解题 和上一题的答案反过来 直接每次在list中第一个位置加入当前层结点 /** * Definition of TreeNode: * public class TreeNode { *

LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9

107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II

给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶节点所在层到根节点所在的层,逐层从左向右遍历)例如:给定二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回其自自底向上的层次遍历为:[  [15,7],  [9,20],  [3]]详见:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/ /** *

【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 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 traver

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 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,null,null,15,7], 3

LeetCode之“树”:Binary Tree Level Order Traversal &amp;&amp; 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 orde