public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
if(root == null)return result;
Queue<TreeNode> que = new LinkedList<TreeNode>();
//result.add(root.val);
que.add(root);
while(!que.isEmpty()){
TreeNode t = que.remove();
if(t.left != null)que.add(t.left);
if(t.right != null)que.add(t.right);
result.add(t.val);
}
return result;
}
二叉树的层级遍历
最新推荐文章于 2023-10-06 09:34:51 发布