关闭

Binary Tree Level Order Traversal

标签: javaleetcodetree层次遍历队列
149人阅读 评论(0) 收藏 举报
分类:

题目描述:

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]
]
层次遍历用队列解决。注意一点是用一个level来存储层次信息,代码如下:

class Node{
	int level;
	TreeNode treenode;
	public Node(int level,TreeNode treenode){
		this.level = level;
		this.treenode = treenode;
	}
}
 
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
		List<List<Integer>> result = new ArrayList<List<Integer>>();
		if(root == null)
			return result;
		ArrayDeque<Node> queue = new ArrayDeque<Node>();
		queue.add(new Node(1, root));
		while(!queue.isEmpty()){
			Node node = queue.pop();
			if(node.level>result.size()){
				List<Integer> list = new ArrayList<Integer>();
				list.add(node.treenode.val);
				result.add(list);
			}else
				result.get(node.level-1).add(node.treenode.val);
			if(node.treenode.left!=null)
				queue.add(new Node(node.level+1,node.treenode.left));
			if(node.treenode.right!=null)
				queue.add(new Node(node.level+1,node.treenode.right));
		}
		return result;
    }
}

0
0
查看评论

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

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51477967Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a b...
  • crazy1235
  • crazy1235
  • 2016-05-26 15:06
  • 2768

102. Binary Tree Level Order Traversal [easy] (Python)

题目链接https://leetcode.com/problems/binary-tree-level-order-traversal/题目原文 Given a binary tree, return the level order traversal of its nodes’ values....
  • coder_orz
  • coder_orz
  • 2016-05-10 14:40
  • 1285

Binary Tree Level Order Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-level-order-traversal/  这道题要求实现树的层序遍历,其实本质就是把树看成一个有向图,然后进行一次广度优先搜索,这个图遍历算法是非常常见的,这里同样是维护一个...
  • linhuanmars
  • linhuanmars
  • 2014-04-11 01:48
  • 13268

【LeetCode with Python】 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 t...
  • nerv3x3
  • nerv3x3
  • 2014-07-06 14:12
  • 1732

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

题目:Binary Tree Level Order Traversal - 二叉树层次遍历 (DFS | BFS) Given a binary tree, return the level order traversal of its nodes' values. (ie, from l...
  • Eastmount
  • Eastmount
  • 2015-09-11 02:20
  • 2754

LeetCode --- 107. 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 ...
  • makuiyu
  • makuiyu
  • 2015-04-17 16:46
  • 1843

LeetCode102 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).
  • l294265421
  • l294265421
  • 2016-04-23 14:20
  • 2893

leetCode解题报告之Binary Tree Level Order Traversal II,I(二叉树层次遍历)

题目: Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fr...
  • u011133213
  • u011133213
  • 2014-03-28 20:28
  • 14688

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 {...
  • Dream_angel_Z
  • Dream_angel_Z
  • 2015-06-13 10:49
  • 844

Binary Tree Zigzag Level Order Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/  这道题其实还是树的层序遍历Binary Tree Level Order Traversal,如果不熟悉的朋友可...
  • linhuanmars
  • linhuanmars
  • 2014-04-26 04:31
  • 13066
    个人资料
    • 访问:62814次
    • 积分:2760
    • 等级:
    • 排名:第15362名
    • 原创:215篇
    • 转载:37篇
    • 译文:0篇
    • 评论:6条
    最新评论