剑指offer - 二叉树的深度(Java实现)
思路:使用递归分治的思想,首先我们可以递归的求出左子树的深度,其次我们可以求出右子树的深度,然后取最大值即可。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
}
} 【剑指offer】题目全解 文章被收录于专栏
本专栏主要是刷剑指offer的题解记录

