LeetCode102 Binary Tree Level Order Traversal Java题解

原创 2015年07月06日 21:13:28

题解:

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

解答:

层序遍历,把一层的节点存在一个队列中,然后依次遍历队列的每一个节点,如果它的子节点不为空继续加入队列中  当队列为空时遍历结束

代码:

public static List<List<Integer>> levelOrder(TreeNode root) {
	   
	   List<List<Integer>> result=new ArrayList<List<Integer>>();//返回的最终结果
	   Queue<TreeNode> treeNodes2=new LinkedList<>();//用来存放每一层的节点
	   
	   //处理第一个节点(root)
	   if(root==null)
		   return result;
	   else {
		   List<Integer> temp=new ArrayList<>();//存放暂时的结果
		   temp.add(root.val);
		   treeNodes2.offer(root);
		   result.add(temp);
		  
	   }
	 
	   while(!treeNodes2.isEmpty())
	   {
		  int i=treeNodes2.size();
		  List<Integer> tempReslut=new ArrayList<>();//存放暂时的结果
		  while(i>0)//遍历这一层的所有节点
		  {
			  
			  TreeNode tNode=treeNodes2.poll();
			  if(tNode.left!=null)
			  {
				  tempReslut.add(tNode.left.val);
				  treeNodes2.offer(tNode.left);
			  }
			  if(tNode.right!=null)
			  {
				  tempReslut.add(tNode.right.val);
				  treeNodes2.offer(tNode.right);
			  }
			  i--;
		  }
		  if(!tempReslut.isEmpty()) result.add(tempReslut);  
	   }
	   
	   return result;
	
    }


相关文章推荐

LeetCode题解-102&107-Binary Tree Level Order Traversal Ⅰ,Ⅱ

原题 102 107 解法 思路分析 与前面很多的题类似,为二叉树的广度优先遍历(BFS),借助一个队列,共N次迭代,N为树的层高,每一次迭代全部第i层的节点出队列,...

LeetCode 102. Binary Tree Level Order Traversal 题解

题目链接:https://leetcode.com/problems/binary-tree-level-order-traversal/?tab=Description 题目要求:按层遍历二叉树,...

[leetcode]102. Binary Tree Level Order Traversal@Java解题报告

https://leetcode.com/problems/binary-tree-level-order-traversal/description/ Given a bina...

LeetCode102 Binary Tree Level Order Traversal Java

题目: Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to r...

leetcode-java-102. Binary Tree Level Order Traversal

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode...

leetcode解题之102&107. Binary Tree Level Order Traversal java版(二叉树层次遍历)

102. Binary Tree Level Order Traversal ,107. Binary Tree Level Order Traversal II ,广度优先遍历, leetcode解...

【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】

【102-Binary Tree Level Order Traversal(二叉树层序遍历)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary...

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

【LeetCode】102. Binary Tree Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51477967Subject 出处:https://leetcode.com/pro...

Leetcode_102_Binary Tree Level Order Traversal

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41929059 Given a binary tree, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode102 Binary Tree Level Order Traversal Java题解
举报原因:
原因补充:

(最多只允许输入30个字)