1/** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10class Solution { 11 12 public int maxDepth(TreeNode root) { 13 if (root == null){ 14 return 0; 15 } 16 17 return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); 18 } 19}
无脑DFS遍历左子树和右子树 然后比较左右哪个大