代码随想录算法训练营Day12

226.翻转二叉树

力扣题目链接:. - 力扣(LeetCode)

DFS(前序递归)

在前序遍历的基础上,实现交换即可


class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        swapNode(root);
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    public void swapNode(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}

BFS

层序遍历

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        Deque<TreeNode> myqueue=new LinkedList<>();
        myqueue.offer(root);
        while(!myqueue.isEmpty()){
            int len=myqueue.size();
            while(len>0){
                TreeNode cur=myqueue.poll();
                swap(cur);
                if(cur.left!=null){
                    myqueue.push(cur.left);
                }
                if(cur.right!=null){
                    myqueue.push(cur.right);
                }
                len--;
            }
        }
        return root;
    }
    public void swap(TreeNode tn){
        TreeNode temp=tn.left;
        tn.left=tn.right;
        tn.right=temp;
    }
}

101. 对称二叉树

力扣题目链接:. - 力扣(LeetCode)

DFS(后序递归)

class Solution {
    public boolean isSymmetric(TreeNode root) {
       return ifsame(root.left,root.right);
    }
    public boolean ifsame(TreeNode left,TreeNode right){
        if(left==null&&right!=null){
            return false;
        }
        else if(right==null&&left!=null){
            return false;
        }
        else if(right==null&&left==null){
            return true;
        }
        else if(right.val!=left.val){
            return false;
        }else{
        boolean out=ifsame(left.left,right.right);
        boolean in=ifsame(left.right,right.left);
            return out&&in;
        }
    }
}

104.二叉树的最大深度

BFS

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        Deque<TreeNode> myqueue=new LinkedList<>();
        myqueue.offer(root);
        int depth=0;
        while(!myqueue.isEmpty()){
            depth++;
            int len=myqueue.size();
            while(len>0){
                TreeNode cur=myqueue.poll();
                if(cur.right!=null){
                    myqueue.offer(cur.right);
                }
                if(cur.left!=null){
                    myqueue.offer(cur.left);
                }
                len--;
            }
        }
        return depth;
    }
}

111.二叉树的最小深度

BFS

class Solution {
    public int minDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        Deque<TreeNode> myqueue=new LinkedList<>();
        myqueue.offer(root);
        int minDepth=0;
        while(!myqueue.isEmpty()){
            int len=myqueue.size();
            minDepth++;
            while(len>0){
            TreeNode cur=myqueue.poll();
            boolean existleft=false;
            boolean existright=false;
            if(cur.left!=null){
                myqueue.offer(cur.left);
                existleft=true;
            }
            if(cur.right!=null){
                myqueue.offer(cur.right);
                existright=true;
            }
            if(!(existleft||existright))
            return minDepth;
            len--;
            }
        }
        return minDepth;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值