题目:二叉树最大深度
思路:dfs或者bfs
代码:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int res;
public int maxDepth (TreeNode root) {
// write code here
dfs(root, 0);
return res;
}
public void dfs (TreeNode root, int num) {
if (root == null) {
res = Math.max(res, num);
return;
}
dfs(root.left, num + 1);
dfs(root.right, num + 1);
}
}