二叉树遍历

public static void levelorder(TreeNode root, int i, ArrayList list){
        if (root == null){
            return;
        }
        int lenght = list.size();//防止溢出
        if (lenght<=i){
            for (int j=0;j<=i-lenght;j++){
                list.add(lenght+j,null);
            }
        }
        list.set(i,root.val);
        levelorder(root.left,2*i,list);
        levelorder(root.right,2*i+1,list);

    }
posted @ 2021-08-02 17:12  落笔生花  阅读(46)  评论(0)    收藏  举报