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

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>
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;//最终的结果
        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();
			last_result.push_back(temp_result);
		}
		return last_result;
    }

int main()
{

}

  

时间: 2024-10-15 11:19:25

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

leetcode 题解: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] ] co

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 le

leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历

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,null,null,15,7],     3    /   9  20     /     15   7 re

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] ] 层序遍历二叉

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

题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / 9 20 / 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ] 挑战 挑战1:只使用一个队列去实现它 挑战2:用DFS算法来做 解题  队列很容易,先加入,然后取出来的同时加入左右孩子节点 在剑指offer中有个题目和这个很类似,其只是层次遍历二叉树,没有要求把每层的节点单独放在一起的. 上面说的规律:每一次打印一

Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

二叉树的层次遍历 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 */ 10 class Solution { 11 public: 12 vector<vector<in

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

给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回其层次遍历结果为:[  [3],  [9,20],  [15,7]]详见:https://leetcode.com/problems/binary-tree-level-order-traversal/description/ /** * Definition for

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

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