代码随想录算法训练营第十六天|104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

101 阅读1分钟

104.二叉树的最大深度

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

思路:求解根节点的高度就是二叉树的最大深度。采用后序遍历的方法来求解根节点的高度。也可以用前序遍历来直接求解二叉树的深度。(层序遍历也可以求解二叉树深度。(迭代法))

后序求解根节点高度

class Solution {
    public int maxDepth(TreeNode root) {
        return getHeight(root);
    }
    public int getHeight(TreeNode node) {
        if (node == null) return 0;
        int leftHeight = getHeight(node.left);
        int rightHeight = getHeight(node.right);
        int nodeHeight = 1 + Math.max(leftHeight, rightHeight);
        return nodeHeight;
    }
}

层序遍历(迭代法)求解深度。

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        int size = 0;
        int deep = 0;
        while (!que.isEmpty()) {
            size = que.size();
            while (size-- > 0) {
                TreeNode node = que.poll();
                if (node.left != null) {
                    que.add(node.left);
                }
                if (node.right != null) {
                    que.add(node.right);
                }
            }
            deep++;
        }
        return deep;
    }
}

559.n叉树的最大深度

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

思路:同样是采用后序递归遍历求解根节点的高度,根节点的高度就是树的最大深度。(同样可以用迭代法(层序遍历)求解,不再贴代码。)

class Solution {
    public int maxDepth(Node root) { // 后序递归求解根节点高度
        return getHeight(root);
    }
    public int getHeight(Node node) {
        if (node == null) return 0;
        List<Integer> heights = new ArrayList<>();
        for(Node child : node.children) {
            heights.add(getHeight(child));
        }
        int nodeHeight = 0;
        for (int height : heights) {
            if (nodeHeight < height) {
                nodeHeight = height;
            }
        }
        return nodeHeight + 1;
    }
}

111.二叉树的最小深度

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

思路:采用后序递归遍历求根节点的最小高度(就是二叉树的最小深度)。也可以使用层序遍历(迭代法)。

class Solution {
    public int minDepth(TreeNode root) { // 后序递归遍历实现。
        return getMinHeight(root);
    }
    public int getMinHeight(TreeNode node) {
        if (node == null) return 0;
        int leftMinHeight = getMinHeight(node.left);
        int rightMinHeight = getMinHeight(node.right);
        // 防止左子树为空,右子树不为空,出现误判
        if (node.left == null && node.right != null) {
            return rightMinHeight + 1;
        }
        // 防止左子树不为空,右子树为空,出现误判
        if (node.left != null && node.right == null) {
            return leftMinHeight + 1;
        }
        return 1 + Math.min(leftMinHeight, rightMinHeight);
    }
}

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

题目链接:222. 完全二叉树的节点个数

层序遍历实现(适用所有二叉树)

class Solution {
    public int countNodes(TreeNode root) { // 采用层序遍历
        if (root == null) return 0;
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        int count = 0;
        while (!que.isEmpty()) {
            TreeNode node = que.poll();
            count++;
            if (node.left != null) {
                que.add(node.left);
            }
            if (node.right != null) {
                que.add(node.right);
            }
        }
        return count;
    }
}

后序递归遍历实现(适用所有二叉树)

class Solution {
    public int countNodes(TreeNode root) { // 后序递归遍历实现
        if (root == null) return 0;
        int countLeft = countNodes(root.left);
        int countRight = countNodes(root.right);
        return 1 + countLeft + countRight;
    }
}

后序递归遍历(只用于完全二叉树)

class Solution {
    public int countNodes(TreeNode root) { // 后序递归遍历实现
        if (root == null) return 0;
        // 根据完全二叉树的特性去左剪枝。
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftLen = 0;
        int rightLen = 0;
        while (left != null) {
            left = left.left;
            leftLen++;
        }
        while (right != null) {
            right = right.right;
            rightLen++;
        }
        if (rightLen == leftLen) {
            return (2 << rightLen) - 1;
        }

        int countLeft = countNodes(root.left);
        int countRight = countNodes(root.right);
        return 1 + countLeft + countRight;
    }
}