递归比较左右子树即可。

import java.util.*;
public class Solution {
    public int maxDepth (TreeNode root) {
        if(root==null)return 0;
        if(root.left==null&&root.right==null)return 1;
        int leftDepth =  maxDepth(root.left)+1;
        int rightDepth =  maxDepth(root.right)+1;
        return leftDepth>rightDepth?leftDepth:rightDepth;
    }
}