剑指 Offer 32 - III. 从上到下打印二叉树 III 剑指 Offer 28. 对称的二叉树 剑指 Offer 27. 二叉树的镜像

剑指 Offer 32 - III. 从上到下打印二叉树 III

/**
 * 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<>();
        if(root==null) return res;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        int count=0;
        while(!queue.isEmpty()){
            count++;
            List<Integer> temp=new ArrayList<>();
            int n=queue.size();
            //从左到右
            if(count%2!=0){
              while(n!=0){
                  TreeNode t1=queue.poll();
                  temp.add(t1.val);
                  if(t1.left!=null) queue.add(t1.left);
                  if(t1.right!=null) queue.add(t1.right);
                  n--;
              }
              res.add(temp);
           }else{
                Stack<Integer> stack=new Stack<>();
              while(n!=0){
                  TreeNode t1=queue.poll();
                  stack.add(t1.val);
                  if(t1.left!=null) queue.add(t1.left);
                  if(t1.right!=null) queue.add(t1.right);
                  n--;
              }

              //注意对stack使用增强for来遍历stack,并不是按后进先出的顺序遍历
            //  for(int num:stack){
             //     temp.add(num);
            //  }
              while(!stack.isEmpty()){
                  temp.add(stack.pop());
              }
              res.add(temp);
            }
            
        }
    return res;
    }
}

剑指 Offer 28. 对称的二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    //思路:按照判定规则
    //对称二叉树定义: 对于树中 任意两个对称节点 LL 和 RR ,一定有:
    //1.此两对称节点值相等。
    //2.即 LL 的 左子节点 和 RR 的 右子节点 对称;
    //3.即 LL 的 右子节点 和 RR 的 左子节点 对称。
    public boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        return isDc(root.left,root.right);

    }
    public boolean isDc(TreeNode l,TreeNode r){
        if(l==null&&r==null) return true;
        if(l==null||r==null||l.val!=r.val)
        return false;
        else return isDc(l.left,r.right)&&isDc(l.right,r.left);
    }
}

剑指 Offer 27. 二叉树的镜像

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        //自己做时还是对如何进行左右节点的交换无头绪,
        if(root==null) return root;
        TreeNode temp_l=mirrorTree(root.left);
        TreeNode temp_r=mirrorTree(root.right);
        root.right=temp_l;
        root.left=temp_r;
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值