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

给定一个二叉树,找出其最大深度。 
二叉树的深度为根节点到最远叶子节点的距离。

如果二叉树为空,则深度为0 
如果不为空,分别求左子树的深度和右子树的深度,去最大的再加1,因为根节点深度是1,要加进去。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxDepth(TreeNode* root) {if (NULL == root)return 0;int l = maxDepth(root->left);int r = maxDepth(root->right);return l > r ? l + 1:r+1;}
};
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int height(TreeNode* root) {    if(root==NULL)    return 0;    else{    int l=height(root->left);    int r=height(root->right);    return 1+((l>r)?l:r);    }    }    int maxDepth(TreeNode* root) {if (NULL == root)return 0;int l = height(root->left);int r = height(root->right);return l > r ? l + 1:r+1;}
};

Maximum Depth of Binary Tree相关推荐

  1. 领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  3. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  4. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  5. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  6. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  7. LC 104. Maximum Depth of Binary Tree

    1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...

  8. LeetCode: 104. Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  9. LeetCode Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

最新文章

  1. python如何爬虫-如何使用python爬虫爬取要登陆的网站
  2. Understand Skills-Based Routing
  3. python解析excel公式_[python][openpyxl]读取excel中公式的结果值
  4. 【pytorch】torch.linspace==>返回一个一维的tensor(张量),这个张量包含了从start到end,分成steps个线段得到的向量
  5. 微信小程序 加载 HTML 标签
  6. 编程通用知识 二叉树
  7. Python:使用ctypes库调用外部DLL
  8. Java 猜单词游戏
  9. Setup Factory 9打包Windows后台服务
  10. matlab神经网络
  11. simm计算机专业英语翻译,计算机专业英语翻译
  12. 被阿里耽误的虾米的一生
  13. 有关古文的C语言编程题,这80道国学题,既经典又有趣!值得珍藏!
  14. 交通领域主要SCI期刊——2017年JCR
  15. iPhone设置中的“开发者”选项
  16. pip runpy.py 报错 pip升级后问题及解决
  17. LinuxProbe学习笔记(二)
  18. FPGA实现ADC采样芯片ADS8688的采样
  19. APP个人开发者月入仅千元,应用开发创业为何这么难?
  20. 《软件工程导论》/ 第一章 软件工程学概述 / 1.4软件过程 / 1.4.1瀑布模型

热门文章

  1. [zz]正则表达式使用详解
  2. 性能测试、负载测试以及压力测试
  3. JQUERY获取text,areatext,radio,checkbox,select值
  4. [转载]基于数据挖掘技术入侵检测系统研究
  5. 关于Java API不能远程访问HBase的问题
  6. 计算机组成和网络考试题,计算机组成原理试题及答案
  7. 【正一专栏】为何我们要侥幸而又苟且地活着
  8. (转载)Web 开发人员需知的 Web 缓存知识
  9. 关于企业信息化中审计流程“寻租”现象的探讨
  10. Leetcode 211. 添加与搜索单词 - 数据结构设计 解题思路及C++实现