二叉树打印

来源:互联网 发布:维基百科数据库下载 编辑:程序博客网 时间:2024/05/22 17:48
import java.util.ArrayList;


public class tmp1 {


public static void main(String[] args) {
TreeNode p1 =null;
}


public static int count = 0;
public static ArrayList<ArrayList<Integer>> row = new ArrayList<ArrayList<Integer>>();


public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
if (pRoot == null)
return row;
else {
depth(pRoot);
return row;
        }
}


public static void depth(TreeNode pRoot) {
count++;
if (count > row.size()) {
ArrayList<Integer> al = new ArrayList<>();
row.add(al);
}
row.get(count - 1).add(pRoot.val);
if (pRoot.left != null)
depth(pRoot.left);
if (pRoot.right != null)
depth(pRoot.right);
count--;
}


public static class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;


public TreeNode(int val) {
this.val = val;


}


}
}
0 0