当前位置:   article > 正文

LeetCode@Tree_100_Max_Depth_B_Tree_b tree leetcode

b tree leetcode

104. Maximum Depth of Binary Tree

  • Total Accepted: 241399
  • Total Submissions: 462901
  • Difficulty: Easy
  • Contributor: LeetCode

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.

Subscribe to see which companies asked this question.

Hide Tags
  Tree Depth-first Search

Java:
  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. {
  13. int h = 0;
  14. if(root == null)
  15. {
  16. return h;
  17. }
  18. else
  19. {
  20. return 1+Math.max(maxDepth(root.left), maxDepth(root.right));
  21. }
  22. }
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/itdef/article/detail/62927
推荐阅读
相关标签
  

闽ICP备14008679号