LeetCode104. 二叉树的最大深度

class Solution {
    public int maxDepth(TreeNode root) {
        /**
         * 方法1:递归
         */
        if (root == null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
        /**
         * 方法2:二叉树的层序遍历
         */
        /*
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            level ++;
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
        }
        return level;
        */
    }
}
原文地址:https://www.cnblogs.com/HuangYJ/p/14169618.html