从上到下打印二叉树(层次遍历)及延伸

  • DFS:采用队列
    几种类型解法类似,不断加约束

1.树从上至下按层打印,同层节点从左至右打印

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 //从上到下打印二叉树的规律:每一次打印一个结点的时候,如果该结点有子结点,则把该结点的子结点放到一个队列的末尾
class Solution {
    public int[] levelOrder(TreeNode root) {
        if(root==null){return new int[0];}  //根为空,返回空列表
        List<Integer> list=new ArrayList<>();  
        Queue<TreeNode> queue=new LinkedList<>();  //包含根节点队列
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode node=queue.poll();   //队首元素出队
            list.add(node.val);
            if(node.left!=null){queue.add(node.left);}
            if(node.right!=null){queue.add(node.right);}
        }
        int[] res=new int[list.size()];
        for(int i=0;i<list.size();i++){
            res[i]=list.get(i);
        }
        return res;
    }
}

2.树按层打印且每一层打印到一行

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    //递归
    List<List<Integer>> res;
    public List<List<Integer>> levelOrder(TreeNode root) {
        res=new ArrayList<>();
        dfs(root,0);
        return res;
    }
    public void dfs(TreeNode root,int depth){
        if(root==null){return;}
        if(res.size()==depth){res.add(new ArrayList<>());}
        res.get(depth).add(root.val);
        dfs(root.left,depth+1);
        dfs(root.right,depth+1);
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
     public List<List<Integer>> levelOrder(TreeNode root) {
         List<List<Integer>> res=new ArrayList<>();
         Queue<TreeNode> queue=new LinkedList<>();
         if(root==null){return res;}
         queue.add(root);
         while(!queue.isEmpty()){
             List<Integer> tmp=new ArrayList<>();
             //不用i++,因为queue的长度在时刻变化,而令i=queue.size(),只需要执行一次
             for(int i=queue.size();i>0;i--){
                 root=queue.poll();
                 tmp.add(root.val);
                 if(root.left!=null){queue.add(root.left);}
                 if(root.right!=null){queue.add(root.right);}
             }
             res.add(tmp);
         }
         return res;
     }
}

3.树按层打印且奇数层正序,偶数层倒序(之字打印)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    //倒序
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        if(root==null){return res;}
        while(!queue.isEmpty()){
            List<Integer> list=new ArrayList<>();
            for(int i=queue.size();i>0;i--){
                // TreeNode node=queue.poll();
                root=queue.poll();
                list.add(root.val);
                if(root.left!=null){queue.add(root.left);}
                if(root.right!=null){queue.add(root.right);}
            }
            if((res.size()&1)==1){Collections.reverse(list);}  //偶数层倒序
            res.add(list);
        }
        return res;
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    //递归
     List<List<Integer>> res;
     public List<List<Integer>> levelOrder(TreeNode root) {
         res=new ArrayList<>();
         dfs(root,0);
         return res;
     }
     public void dfs(TreeNode root,int depth){
         if(root==null){return;}
         if(res.size()==depth){
             res.add(new ArrayList<>());
         }
         if((depth&1)==0){res.get(depth).add(root.val);}
         if((depth&1)==1){res.get(depth).add(0,root.val);}
         dfs(root.left,depth+1);
         dfs(root.right,depth+1);
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值