https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
http://blog.csdn.net/linhuanmars/article/details/23404111
/** * definition for binary tree * public class treenode { * int val; * TreeNode left; * treenode right; * treenode (int &NBSP;X) { val = x; } * } */public class solution { public list<list<integer>> levelorder (TreeNode root) { // this is how we use queue to iterate a tree. // To let nodes on the same level be returned in the same list, we can store an extra level for each nodes. list<list< Integer>> result = new arraylist<> (); if (root == null) return result; queue<nodewithlevel> queue = new LinkedList<> (); queue.offer (new nodewithlevel (root, 0)); while (!queue.isEmpty ( )) { nodewithlevel nodewithlevel = queue.poll (); if (Nodewithlevel.level == result.size ()) &nbSp; result.add (new arraylist<integer> ()); result.get (NodeWithLevel.level). Add ( NodeWithLevel.node.val); if (nodewithlevel.node.left != null ) { queue.offer (New nodewithlevel ( nodewithlevel.node.left, nodewithlevel.level + 1)); } if (Nodewithlevel.node.right != null) { &nbsP; queue.offer (New nodewithlevel ( nodewithlevel.node.right, nodewithlevel.level + 1)); } } return result; } private static class NodeWithLevel { TreeNode node; int level; nodewithlevel (TreeNode Node, int level) { this.node = node; this.level = level; } }}
[leetcode]102 Binary Tree level Order traversal