二叉树的层序遍历
从上往下打印二叉树
http://www.nowcoder.com/questionTerminal/7fe2212963db4790b57431d9ed259701
ArrayList<Integer> list = new ArrayList<Integer>();
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
if (root == null) return list;
list.add(root.val);
bfs(root);
return list;
}
public void bfs(TreeNode root) {
if (root == null) return;
if (root.left != null) {
list.add(root.left.val);
}
if (root.right != null) {
list.add(root.left.val);
}
bfs(root.left);
bfs(root.right);
} 二叉树的层序遍历