鱼C论坛

 找回密码
 立即注册
查看: 1186|回复: 0

[学习笔记] leetcode 104. Maximum Depth of Binary Tree

[复制链接]
发表于 2019-8-19 10:15:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  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. /**
  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. class Solution {
  11.    
  12.     public int maxDepth(TreeNode root) {
  13.         
  14.         if(root == null){
  15.             
  16.             return 0;
  17.         }
  18.         
  19.         return generateInt(root);
  20.     }
  21.    
  22.     public int generateInt(TreeNode root){
  23.         
  24.         if(root != null)
  25.             return (1 + Math.max(generateInt(root.left),generateInt(root.right)));
  26.         else
  27.             return 0;
  28.         
  29.     }
  30. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 07:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表