算法刷刷刷| 二叉树篇| 二叉树的属性篇| 226翻转二叉树|101对称二叉树| 104二叉树的最大深度| 559N叉树最大深度| 111最小深度| 222完全二叉树的节点个数

在这里插入图片描述

226.翻转二叉树

在这里插入图片描述

public TreeNode invertTree(TreeNode root) {
        TreeNode res = root;
        dfs(root);
        return res;
    }

    private void dfs(TreeNode node) {
        if (node == null) {
            return;
        }
        TreeNode tmp = node.left;
        node.left = node.right;
        node.right = tmp;
        dfs(node.left);
        dfs(node.right);
    }

101.对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return isSame(root.left, root.right);
    }

    private boolean isSame(TreeNode a, TreeNode b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        if (a.val == b.val) {
            return isSame(a.left, b.right) && isSame(a.right, b.left);
        }
        return false;
    }
}

时间复杂度o(n),相当于把每个节点都遍历了一遍
空间复杂度o(n) 最坏是个人字形树,栈空间最多是n层

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return false;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root.left);
        queue.add(root.right);
        while (!queue.isEmpty()) {
            TreeNode a = queue.poll();
            TreeNode b = queue.poll();
            if (a == null && b == null) {
                continue;
            }
            if (a == null || b == null || a.val != b.val) {
                return false;
            }
            queue.add(a.left);
            queue.add(b.right);
            queue.add(a.right);
            queue.add(b.left);
        }
        return true;
    }
}

这里要注意Queue< TreeNode> queue = new LinkedList<>();不能是new ArrayDeque<>(),ArrayDeque不能添加null
时间复杂度o(n)
空间复杂度o(n),队列中最多n个节点

104.二叉树的最大深度

public int maxDepth(TreeNode node) {
        if (node == null) {
            return 0;
        }
        if (node.left == null && node.right == null) {
            return 1;
        }
        int leftD = maxDepth(node.left);
        int rightD = maxDepth(node.right);
        if (leftD == 0 || rightD == 0) {
            return leftD + rightD + 1;
        }
        return Math.max(leftD, rightD) + 1;
    }

559.N叉树的最大深度

给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)

在这里插入图片描述

class Solution {
    public int maxDepth(Node node) {
        if (node == null) {
            return 0;
        }
        int max = 0;
        for (Node child : node.children) {
            int d = maxDepth(child);
            max = Math.max(max, d);
        }
        return max+1;
    }
}

111.二叉树的最小深度

给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。

图片来自卡哥代码随想录

public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (root.left == null || root.right == null) {
            return leftDepth + rightDepth + 1;
        }
        return Math.min(leftDepth, rightDepth) + 1;
    }

卡哥的版本更好理解:

class Solution {
    /**
     * 递归法,相比求MaxDepth要复杂点
     * 因为最小深度是从根节点到最近**叶子节点**的最短路径上的节点数量
     */
    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;
        }
        // 左右结点都不为null
        return Math.min(leftDepth, rightDepth) + 1;
    }
}

这里有一个误区,开始写的时候没有注意,如果按照求最大深度的思路写的话,会写出下面这样的代码:

        int leftD = minDepth(node.left);
        int rightD = minDepth(node.right);
        if (leftD == 0 || rightD == 0) {
            return leftD + rightD + 1;
        }
        return Math.min(leftD, rightD) + 1;

但是这样的话,如果一个节点的左子树不存在,右子树存在,那么返回的就是0+1了,但是题目要求的是根节点到叶子节点的最少节点个数,所以应该返回的是右子树最少个数+1,要注意区别。

222.完全二叉树的最小个数

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

在这里插入图片描述

public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        int res = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            res += size;
            while (size-- > 0) {
                TreeNode tmp = queue.poll();
                if (tmp.left != null) {
                    queue.add(tmp.left);
                }
                if (tmp.right != null) {
                    queue.add(tmp.right);
                }
            }
        }
        return res;
    }
class Solution {
    public int countNodes(TreeNode root) {
        return count(root, 0);
    }

    private int count(TreeNode root, int num) {
        if (root == null) {
            return 0;
        }
        return count(root.left, num) + count(root.right, num) + 1;
    }
}

利用了完全二叉树的性质:

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftD = 0;
        int rightD = 0;
        while (left != null) {
            left = left.left;
            leftD++;
        }
        while (right != null) {
            right = right.right;
            rightD++;
        }
        if (leftD == rightD) {
            return (int) (Math.pow(2, leftD + 1) - 1);
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

对于每一个节点,都判断下这个节点为根的树是不是个完全二叉树,根据完全二叉树的定义,只要一直遍历左节点个数等于右节点个数就是完全二叉树,确定了是的话就可以用2^n-1公式求当前节点为根的完全二叉树的节点个数了**(这个n指的是树的深度,以上代码中leftD是数了当前节点下面的左节点个数,还得加1才是这个树的深度)**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值