104. Maximum Depth of Binary Tree (二叉树最大深度)

朴灿烈づ我的快乐病毒、 2022-07-15 01:17 258阅读 0赞

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public int maxDepth(TreeNode root) {
  12. if(root == null)
  13. return 0;
  14. else{
  15. int l = maxDepth(root.left);
  16. int r = maxDepth(root.right);
  17. return l>r?l+1:r+1;
  18. }
  19. }
  20. }

发表评论

表情:
评论列表 (有 0 条评论,258人围观)

还没有评论,来说两句吧...

相关阅读