方法1:python
莫名其妙做对了。。
本人还处于懵逼阶段,好像悟了,又好像没悟 喵呜
不过代码不是很简洁 喵呜
class Solution:
def maxDepth(self , root ):
# write code here
#base case
if root==None: return 0
cnt1=self.maxDepth(root.left)
cnt2=self.maxDepth(root.right)
return 1+max(cnt1,cnt2)
方法2:java
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 maxDepth (TreeNode root) {
// write code here
if (root==null) return 0;
int cnt1=maxDepth(root.left);
int cnt2=maxDepth(root.right);
return 1+Math.max(cnt1,cnt2);
}
}