zoukankan      html  css  js  c++  java
  • 23:从上往下打印二叉树

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    
    /**
     * 面试题23:从上往下打印二叉树
     * 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
     */
    public class _23_print_binary_tree {
        public static void main(String[] args) {
            TreeNode23 treeNode23 = new TreeNode23(1);
            treeNode23.left=new TreeNode23(2);
            treeNode23.right=new TreeNode23(3);
            treeNode23.left.left=new TreeNode23(4);
            treeNode23.left.right=new TreeNode23(5);
            Solution23 solution23 = new Solution23();
            ArrayList<Integer> al=solution23.PrintFromTopToBottom(treeNode23);
            for(Integer spl:al){
                System.out.print(spl+"、");
            }
        }
    }
    
    class Solution23 {
        public ArrayList<Integer> PrintFromTopToBottom(TreeNode23 root) {
            ArrayList<Integer> al = new ArrayList<Integer>();
            Queue<TreeNode23> queue = new LinkedList<TreeNode23>();
            if(root==null){
                return al;
            }
            queue.add(root);
            while(!queue.isEmpty()){
                TreeNode23 tmp=queue.poll();
                al.add(tmp.val);
                if(tmp.left!=null){
                    queue.add(tmp.left);
                }
                if(tmp.right!=null){
                    queue.add(tmp.right);
                }
            }
            return al;
        }
    }
    class TreeNode23 {
        int val = 0;
        TreeNode23 left = null;
        TreeNode23 right = null;
        public TreeNode23(int val) {
            this.val = val;
        }
    }
    
  • 相关阅读:
    python中的继承和多态
    python中的深浅copy
    面向对象初识
    常用模块,异常处理
    递归,re,time,random
    内置函数,匿名函数
    生成器和迭代器
    记一次nginx由于文件过大的相关问题
    vue的Element+gin实现文件上传
    Vue问题汇总
  • 原文地址:https://www.cnblogs.com/andy-zhou/p/6549583.html
Copyright © 2011-2022 走看看