第六章 二叉树part03

96 阅读1分钟

104. Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

题目解析:

  • 最大高度可以使用层次遍历
  • 可以使用递归或迭代求解

代码:
1. 迭代

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

2. 递归

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

111. Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

题目解析:

  • 求根节点到叶子节点的最短路径可以使用迭代或递归解决

使用迭代时,非根节点为0时不能直接返回0, 因为是取最小值,只需左右节点其中一个为空就会返回0,不能保证父节点一定为叶子节点

代码: 1. 递归

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        return min(root);
    }

    public int min(TreeNode root) {
        if (root == null) {
            return Integer.MAX_VALUE;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return Math.min(min(root.left), min(root.right)) + 1;
    }
}

2. 迭代

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

222. Count Complete Tree Nodes

Given the root of a complete binary tree, return the number of the nodes in the tree.

According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

Design an algorithm that runs in less than O(n) time complexity.

题目解析:

  • 迭代的方式通过层次遍历求出二叉树的高以及最后一层所有叶子结点的个数
  • 递归的方式通过判断子树最左边的高度和最后边的高度是否相等,如果相等根据完全树的性质求概述的节点个数,如果不相等,则递归分别按相同方法求左右子树的个数

代码: 1. 迭代

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        int depth = 0;
        int size = 0;
        while (!q.isEmpty()) {
            depth++;
            size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = q.poll();
                if (node.left != null) q.add(node.left);
                if (node.right != null) q.add((node.right));
            }
        }
        return (int)Math.pow(2, depth - 1) - 1 + size;
    }
}

2. 递归

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

总结

  1. 求二叉树的高度优先使用层次遍历
  2. 一般二叉树的问题可以使用递归(优先考虑)和迭代解决