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

解题思路:

运用广度优先搜索方法,同http://www.cnblogs.com/zihaowang/p/5149745.html类似。

代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(root == null)
        	return result;
        queue.offer(root);
        while(!queue.isEmpty()){
        	int m = queue.size();
        	List<Integer> list = new LinkedList<Integer>();
        	for(int i = 0; i < m; i++){
        		if(queue.peek().left != null)
        			queue.offer(queue.peek().left);
        		if(queue.peek().right != null)
        			queue.offer(queue.peek().right);
        		list.add(queue.poll().val);
        	}
        	result.add(new LinkedList<Integer>(list));
        }
        return result;
    }
}

  

时间: 2024-10-13 16:16:53

Java [Leetcode 102]Binary Tree Level Order Traversal的相关文章

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—102 Binary Tree Level Order Traversal(二叉树的层次遍历)

Binary Tree Level Order Traversal Total Accepted: 51429 Total Submissions: 174478 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},

Java for 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] ] 解题思路:

[LeetCode] 102. Binary Tree Level Order Traversal Java

题目: 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 return its level order traversal as: [ [3], [9,20], [15,7

Java [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

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

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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

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

[宽度优先搜索] leetcode 102 Binary Tree Level Order Traversal

problem:https://leetcode.com/problems/binary-tree-level-order-traversal 二刷此题,现在看起来是基础中的基础题了. class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if(!root) return res; queue<TreeNode