算法笔记|Day12二叉树II

☆☆☆☆☆leetcode 226.翻转二叉树

题目链接:leetcode 226.翻转二叉树

题目分析

只要把每一个节点的左右子节点翻转一下,就可以达到整体翻转的效果。可以采用递归方法,推荐前序遍历和后序遍历,中序遍历也可以解决,但需要改进代码逻辑;同样可以采用迭代方法,进行层序遍历。

代码

1.递归,前序遍历
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)
            return null;
        invertTree(root.left);
        invertTree(root.right);
        swapChildern(root);
        return root;
    }
    
    public void swapChildern(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}
2.递归,后序遍历
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)
            return null;
        swapChildern(root);
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    
    public void swapChildern(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}
3.递归,后序遍历
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)
            return null;
        invertTree(root.left);
        swapChildern(root);
        invertTree(root.left);
        return root;
    }
    
    public void swapChildern(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}
4.迭代,层序遍历
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)
            return null;
        Deque<TreeNode> deque=new ArrayDeque<>();
        deque.add(root);
        while(!deque.isEmpty()){
            int size=deque.size();
            for(int i=0;i<size;i++){
                TreeNode node=deque.poll();
                swapChildren(node);
                if(node.left!=null)
                    deque.add(node.left);
                if(node.right!=null)
                    deque.add(node.right);
            }
        }
        return root;
    }

    public void swapChildren(TreeNode root){
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}

☆☆☆☆☆leetcode 101. 对称二叉树

题目链接:leetcode 101. 对称二叉树

题目分析

同时处理左右两个子树,分别比较内侧和外侧节点,递归采用后序遍历(事实上是一个树的遍历顺序是左右中,一个树的遍历顺序是右左中),另外可以采用迭代法层序遍历。

代码

1.1递归,后序遍历
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left,root.right);
    }

    public boolean compare(TreeNode left,TreeNode right){
        if(left==null&&right==null)
            return true;
        if(left!=null&&right==null)
            return false;
        if(left==null&&right!=null)
            return false;
        if(left.val!=right.val)
            return false;
        boolean compareOutside=compare(left.left,right.right);
        boolean compareInside=compare(left.right,right.left);
        return compareOutside&&compareInside;
    }
}
1.2递归,后序遍历(精简版本)
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left,root.right);
    }

    public boolean compare(TreeNode left,TreeNode right){
        if(left==null&&right==null)
            return true;
        if(left==null||right==null||left.val!=right.val)
            return false;
        return compare(left.left,right.right)&&compare(left.right,right.left);
    }
}
2.迭代,层序遍历
class Solution {
    public boolean isSymmetric(TreeNode root) {
        Deque<TreeNode> deque=new LinkedList<>();
        deque.offerFirst(root.left);
        deque.offerLast(root.right);
        while(!deque.isEmpty()){
            TreeNode left=deque.pollFirst();
            TreeNode right=deque.pollLast();
            if(left==null&&right==null)
                continue;
            if(left==null||right==null||left.val!=right.val)
                return false;
            deque.offerFirst(left.left);
            deque.offerFirst(left.right);
            deque.offerLast(right.right);
            deque.offerLast(right.left);
        }
        return true;
    }
}

☆☆☆leetcode 100.相同的树(待补充)

题目链接:leetcode 100.相同的树

题目分析

代码


☆☆☆leetcode 572.另一个树的子树(待补充)

题目链接:leetcode 572.另一个树的子树

题目分析

代码


☆☆☆☆☆leetcode 104.二叉树的最大深度

题目链接:leetcode 104.二叉树的最大深度

题目分析

二叉树的最大深度也就是根节点的高度,递归采用后序遍历,另外也可以采用迭代层序遍历。

代码

1.1递归,后序遍历
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        int leftDepth=maxDepth(root.left);
        int rightDepth=maxDepth(root.right);
        int depth=Math.max(leftDepth,rightDepth)+1;
        return depth; 
    }
}
1.2递归,后序遍历(精简版本)
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        return Math.max(maxDepth(root.left),maxDepth(root.right))+1; 
    }
}
3.迭代,层序遍历
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        Deque<TreeNode> deque=new LinkedList<>();
        deque.add(root);
        int depth=0;
        while(!deque.isEmpty()){
            int size=deque.size();
            depth++;
            for(int i=0;i<size;i++){
                TreeNode Node=deque.poll();
                if(Node.left!=null)
                    deque.add(Node.left);
                if(Node.right!=null)
                    deque.add(Node.right);
            }
        }
        return depth;
    }
}

☆☆☆☆☆leetcode 559.n叉树的最大深度(待补充)

题目链接:leetcode 559.n叉树的最大深度

题目分析

代码


☆☆☆☆☆leetcode 111.二叉树的最小深度

题目链接:leetcode 111.二叉树的最小深度

题目分析

二叉树的最小深度也就是从根节点到最近叶子节点的距离,递归采用后序遍历,另外也可以采用迭代层序遍历。

代码

1.1递归,后序遍历
class Solution {
    public int minDepth(TreeNode root) {
        if(root==null)
            return 0;
        int leftDepth=minDepth(root.left);
        int rightDepth=minDepth(root.right);
        if(root.left==null)
            return rightDepth+1;
        if(root.right==null)
            return leftDepth+1;
        int depth=Math.min(leftDepth,rightDepth)+1;
        return depth; 
    }
}
1.2递归,后序遍历(精简版本)
class Solution {
    public int minDepth(TreeNode root) {
        if(root==null)
            return 0;
        if(root.left==null)
            return minDepth(root.right)+1;
        if(root.right==null)
            return minDepth(root.left)+1;
        return Math.min(minDepth(root.left),minDepth(root.right))+1; 
    }
}
3.迭代,层序遍历
class Solution {
    public int minDepth(TreeNode root) {
        if(root==null)
            return 0;
        Deque<TreeNode> deque=new LinkedList<>();
        deque.add(root);
        int depth=0;
        while(!deque.isEmpty()){
            int size=deque.size();
            depth++;
            for(int i=0;i<size;i++){
                TreeNode Node=deque.poll();
                if(Node.left==null&&Node.right==null)
                    return depth;
                if(Node.left!=null)
                    deque.add(Node.left);
                if(Node.right!=null)
                    deque.add(Node.right);
            }
        }
        return depth;
    }
}
  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值