一、题目内容

image.png

二、题解

解法1:

思路

dfs,求左右子树高度+1

代码

  1. public class Solution {
  2. /**
  3. *
  4. * @param root TreeNode类
  5. * @return int整型
  6. */
  7. public int maxDepth (TreeNode root) {
  8. // write code here
  9. if(root == null){
  10. return 0;
  11. }
  12. return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
  13. }
  14. }