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

这道题的意思是树的层序遍历,用到了队列,每打印一个节点的时候,如果这个结点有左右子树,则将其左右子树加入队列。由于输出的形式是每一层相当于list中的一个元素,因此在程序中每一层都用到了for循环,貌似图的广度优先也是用的队列代码如下:
public List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList result = new ArrayList();
		if(root==null){
			return result;
		}
		Queue<TreeNode> q = new LinkedList<TreeNode>();
		q.offer(root);
		while(!q.isEmpty()){
			ArrayList<Integer> lever = new ArrayList<Integer>();
			int size = q.size();
			for(int i = 0;i<size;i++){
				TreeNode temp = q.poll();
				lever.add(temp.val);
				if(temp.left!=null){
					q.offer(temp.left);
				}
				if(temp.right!=null){
					q.offer(temp.right);
				}
			}
			result.add(lever);
		}
		return result;
    }

  队列的方法有如下几种:

add        增加一个元索                     如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove   移除并返回队列头部的元素    如果队列为空,则抛出一个NoSuchElementException异常
element  返回队列头部的元素             如果队列为空,则抛出一个NoSuchElementException异常
offer       添加一个元素并返回true       如果队列已满,则返回false
poll         移除并返问队列头部的元素    如果队列为空,则返回null
peek       返回队列头部的元素             如果队列为空,则返回null
put         添加一个元素                      如果队列满,则阻塞
take        移除并返回队列头部的元素     如果队列为空,则阻塞

时间: 2024-10-13 12:21:10

leetcode之Binary Tree Level Order Traversal的相关文章

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

Java for LeetCode 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,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【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] ] 思路:使用

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

Leetcode 树 Binary Tree Level Order Traversal II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal II Total Accepted: 10080 Total Submissions: 32610 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, l

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 树 Binary Tree Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal Total Accepted: 12292 Total Submissions: 40417 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level

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][JavaScript]Binary Tree Level Order Traversal

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

LeetCode(32)-Binary Tree Level Order Traversal

题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary Tree Level Order Traversal My Submissions QuestionEditorial Solution Total Accepted: 98313 Total Submissions: 302608 Difficulty: Easy Given a binary