代码随想录算法训练营Day15

226.翻转二叉树 ,101. 对称二叉树 ,104.二叉树的最大深度,111.二叉树的最小深度,222.完全二叉树的节点个数

226.翻转二叉树力扣题目链接

只支持前序,后序。        中序的不可以 

//DFS
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null)return null;
        swap(root);                    //输入的根节点
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    public void  swap(TreeNode root){
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    }
}

递归三部曲:

  1. 确定递归函数的参数和返回值
    TreeNode invertTree(TreeNode root)
  2. 确定终止条件   
    if (root == NULL) return root;
  3. 确定单层递归的逻辑
swap(root);
invertTree(root->left);
invertTree(root->right);

//BFS
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {return null;}
        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            while (size-- > 0) {
                TreeNode node = deque.poll();
                swap(node);
                if (node.left != null) deque.offer(node.left);
                if (node.right != null) deque.offer(node.right);
            }
        }
        return root;
    }

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

101. 对称二叉树力扣题目链接

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 false;
        }
        if(left != null && right == null){
            return false;
        }
        if(left == null && right ==null){    //只有递归到null才为真
            return true;
        }
        if(left.val != right.val ){
            return false;
        }
        // 此时就是:左右节点都不为空,且数值相同的情况
        // 此时才做递归,做下一层的判断
        //遍历两棵树而且要比较内侧和外侧节点
        boolean out = compare(left.left,right.right);
        boolean in = compare(left.right,right.left);
        return out && in;
    }
}

节点为空的情况有:(注意我们比较的其实不是左孩子和右孩子,所以如下我称之为左节点右节点

  • 左节点为空,右节点不为空,不对称,return false
  • 左不为空,右为空,不对称 return false
  • 左右都为空,对称,返回true

此时已经排除掉了节点为空的情况,那么剩下的就是左右节点不为空:

  • 左右都不为空,比较节点数值,不相同就return false

104.二叉树的最大深度力扣题目链接

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null)return 0;
        int i = maxDepth(root.left);
        int j = maxDepth(root.right);
        return i > j ? i+1 : j+1;
    }
}

终止条件 节点为null

因为计算深度时,没算上根节点,所以最后要+1;

111.二叉树的最小深度力扣题目链接

迭代法 

class Solution {
    public int minDepth(TreeNode root){
       if(root == null)return 0;
       Queue<TreeNode> que = new LinkedList<>();
       que.offer(root);
       int depth = 0;
       while(!que.isEmpty()){
           int size = que.size();
           depth++;
           for(int i = 0; i < size ; i++){
               TreeNode node = que.poll();
               if(node.right == null && node.left ==null){
                   return depth;
               }
               if(node.left != null)que.offer(node.left);
               if(node.right != null)que.offer(node.right);
           }
       }
       return depth;
    }
}

递归法

class Solution {
    public int minDepth(TreeNode root){
       if(root == null)return 0;
       int i = minDepth(root.left);
       int j = minDepth(root.right);
       // 当一个左子树为空,右不为空,这时并不是最低点
       if(root.left == null){
           return j + 1;
       }
       // 当一个右子树为空,左不为空,这时并不是最低点
       if(root.right == null){
           return i +1;
       }
        return Math.min(i, j) + 1;
    }
}

求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。


222.完全二叉树的节点个数

力扣题目链接

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null)return 0;
        return countNodes(root.left)+countNodes(root.right)+1;
    }
}

终止条件 节点等于null

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值