import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
private int maxDep = 0;
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
// write code here
this.dfs(root, 0);
return maxDep;
}
public void dfs(TreeNode node, int depth) {
if (node == null) {
return;
}
depth++;
if (node.left == null && node.right == null) {
maxDep = Math.max(depth, maxDep);
}
dfs(node.left, depth);
dfs(node.right, depth);
}
}
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
private int maxDep = 0;
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
// write code here
this.dfs(root, 0);
return maxDep;
}
public void dfs(TreeNode node, int depth) {
if (node == null) {
return;
}
depth++;
if (node.left == null && node.right == null) {
maxDep = Math.max(depth, maxDep);
}
dfs(node.left, depth);
dfs(node.right, depth);
}
}