LeetCode: 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 as:

[
  [3],
  [9,20],
  [15,7]
]

题目解答:

/**
	 * 实质是二叉树的广度优先搜索
	 * 利用一个辅助队列保存被访问的当前节点的左右孩子,
	 * 调整进出队列的顺序以实现层序遍历
	 */
	public List<List<Integer>> levelOrder(TreeNode root) {
		 List<List<Integer>> result=new ArrayList<>();

         if(root==null){
        	return result;
         }

        /**
         * 一般来说,队列的实现选择LinkedList即可
         * 队列保存节点就不用找什么变量了
         */
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        //首先把头结点的值保存到结果集,然后把左右子节点分别进入队列
       while(!queue.isEmpty()){//需要使用isEmptyy判断,不能使用null
    	   List<Integer> temp=new ArrayList<>();
    	   /**
    	    * error!这里对进行循环的过程,队列长度是在不断变化的
    	    * size需要等于队列出队前的长度
    	    */
    	   int size =queue.size();
    	   for(int i=0;i<size;i++){
    		   TreeNode node=queue.poll();
    		   temp.add(node.val);
    		   if(node.left!=null)
    		     queue.offer(node.left);
    		   if(node.right!=null)
    		     queue.offer(node.right);
    	   }
    	   //当前队列全部poll,到这里已经完成了一层的遍历
    	   result.add(temp);
       }
		return result;
    }

  

时间: 2024-10-05 03:51:12

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树的相关文章

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

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

[题目] 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

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/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

[LeetCode]Binary Tree Level Order Traversal II

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 re

LeetCode: 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   7return its leve

leetcode Binary Tree Level Order Traversal I II

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