leetcode题记:Maximum Depth of Binary Tree

悠悠 2022-05-18 07:57 184阅读 0赞

编程语言:JAVA

题目描述:

  1. Given a binary tree, find its maximum depth.
  2. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
  3. Note: A leaf is a node with no children.
  4. Example:
  5. Given binary tree [3,9,20,null,null,15,7],
  6. 3
  7. / \
  8. 9 20
  9. / \
  10. 15 7
  11. return its depth = 3.

提交反馈:

  1. 39 / 39 test cases passed.
  2. Status: Accepted
  3. Runtime: 0 ms
  4. Submitted: 0 minutes ago
  5. You are here!
  6. Your runtime beats 100.00 % of java submissions.

解题思路:

  1. 知道这是一道递归题目,但是就是写不出递归程序,太菜了。。。。
  2. 继续练习吧。。。。。。。。。。。。。。。。。。。。。。。。。。。

代码:

  1. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
  2. class Solution {
  3. public int maxDepth(TreeNode root) {
  4. if(root == null){
  5. return 0;}
  6. return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
  7. }
  8. }

发表评论

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

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

相关阅读