zoukankan      html  css  js  c++  java
  • 面试题32:从上到下打印二叉树

    层次遍历二叉树

    import java.util.ArrayList;
     
    import java.util.List;
    import java.util.Queue;
    import java.util.concurrent.LinkedBlockingDeque;
    /**
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
     
        public TreeNode(int val) {
            this.val = val;
     
        }
     
    }
    */
    public class Solution {
         public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
            List<Integer> list = new ArrayList<Integer>();
             if(root==null)
                return (ArrayList<Integer>) list;
            Queue<TreeNode> queue = new LinkedBlockingDeque<TreeNode>();
            queue.add(root);
            while(queue.size()>0){
                TreeNode node05 = queue.poll();
                list.add(node05.val);
                if(node05.left!=null)
                    queue.add(node05.left);
                if(node05.right!=null)
                    queue.add(node05.right);
            }
            return (ArrayList<Integer>) list;
        }
    }
  • 相关阅读:
    网页导出PDF文件
    图片翻转导航
    瀑布流之ajax
    楼梯效果
    数码时钟
    数字字母随机验证码
    kafka shell
    zookeeper shell
    正则
    Linux(一)
  • 原文地址:https://www.cnblogs.com/Allen-win/p/8746526.html
Copyright © 2011-2022 走看看