Leetcode-104. 二叉树的最大深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回它的最大深度 3 。

解法:介绍两种算法,BFS和DFS(使用Recursion实现)

BFS

  • Java
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
   
    public int maxDepth(TreeNode root) {
   
        if(root==null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int size = 0;
        int level = 0;
        while(!queue.isEmpty()){
   
            size = queue.size();
            for(int i=0;i<size;i++){
   
                TreeNode node = queue.poll();
                if(node.left!=null) queue.offer(node.left);
                if(node.right!=null) queue.offer(node.right);
            }
            level++;
        }
        return level;
    }
}
  • Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if not root: return 0
        queue = []
        size, level = 0, 0
        queue.append(root)
        while(queue):
            size = len(queue)
            for _ in range(size):
                node = queue.pop(0)
                if node.left: queue.append(node.left)
                if node.right: queue.append(node.right)
            level += 1
        return level

DFS

  • Java
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
   
    public int maxDepth(TreeNode root) {
   
        return root==null ? 0 : 1+Math.max(this.maxDepth(root.left),this.maxDepth(root.right));
    }
}
  • Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if not root: return 0
        return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))

Leetcode-111. 二叉树的最小深度

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回它的最小深度  2.

解法:类似上题

BFS

  • Java
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
   
    public int minDepth(TreeNode root) {
   
        if(root==null) return 0;
        int size = 0;
        int level = 1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(queue!=null){
   
            size = queue.size();
            for(int i=0;i<size;i++){
   
                TreeNode node = queue.poll();
                if(node.left==null && node.right==null) return level;
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
            }
            level++;
        }
        return level;
    }
}
  • Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if not root: return 0
        size , level = 0, 1
        queue = []
        queue.append(root)
        while(queue):
            size = len(queue)
            for _ in range(size):
                node = queue.pop(0)
                if not node.left and not node.right: return level
                if node.left: queue.append(node.left)
                if node.right: queue.append(node.right)
            level += 1
        return level

DFS

  • Java
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
   
    public int minDepth(TreeNode root) {
   
        if(root==null) return 0;
        int minLeft = this.minDepth(root.left);
        int minRight = this.minDepth(root.right);
        return (minLeft==0 || minRight==0)? minLeft+minRight+1: 1+Math.min(minLeft,minRight);
    }
}
  • Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if not root: return 0
        minLeft = self.minDepth(root.left)
        minRight = self.minDepth(root.right)
        if minLeft==0 or minRight==0:
            return minLeft+minRight+1
        else:
            return 1+min(minLeft,minRight)