dfs遍历每一条路径,比较其大小即可。 public class Solution { int max = 0; public void dfs(TreeNode root, int deepth){ if(root==null){ if(deepth>max){ max = deepth; } return; } dfs(root.left, deepth+1); dfs(root.right, deepth+1); } public int TreeDepth(TreeNode root) { dfs(root, 0); return max; } }