题意:给一棵二叉树,求其深度。

思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1。

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int maxDepth(TreeNode* root) {
  13. if(!root) return ;
  14. int a=maxDepth(root->left);
  15. int b=maxDepth(root->right);
  16. return a>b?a+:b+;
  17. }
  18. };

AC代码

LeetCode Maximum Depth of Binary Tree (求树的深度)的更多相关文章

  1. LeetCode——Maximum Depth of Binary Tree

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

  2. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

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

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  7. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

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

  8. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode:Maximum Depth of Binary Tree【Python版】

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

随机推荐

  1. Spark Streaming揭秘 Day5 初步贯通源码

    Spark Streaming揭秘 Day5 初步贯通源码 引子 今天,让我们从Spark Streaming最重要的三个环节出发,让我们通过走读,逐步贯通源码,还记得Day1提到的三个谜团么,让我们 ...

  2. gc overhead limit exceeded

    eclipse-- gc overhead limit exceeded 修改内存不足的方法如下: Eclipse报错:gc overhead limit exceeded eclipse 原因是Ec ...

  3. Java学习之路:1、HelloWorld

    似乎每种语言都是从HelloWorld开始的,所以,我的第一个java程序,也应该是这样开始的! 1.配置好jdk后,开始编写HelloWorld.java package second;//这个应该 ...

  4. 搭建Git Server

    windows上如何搭建Git Server   Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用.那么私有 ...

  5. C# 写XML文件

    /// <summary>x /// 修改xml文件 /// </summary> /// <param name="dt"></para ...

  6. 域名的a记录转过来他的公网ip

    首先 客户要我把域名 和项目进行绑定客户需要提供万网或者新网的账户,登进去域名管理  选项域名管理的A记录定向到那个公网ip上 与服务器做绑定然后 在服务器的iis 上 加个主机头 输入主机头名称 也 ...

  7. Rust入门篇 (1)

    Rust入门篇 声明: 本文是在参考 The Rust Programming Language 和 Rust官方教程 中文版 写的. 个人学习用 再PS. 目录这东东果然是必须的... 找个时间生成 ...

  8. 4.0 spring-注册解析的Bean

    1.0 registerBeanDefinition 对于配置文件,解析也解析完了,装饰也装饰完了,对于得到的BeanDefinition已经可以满足后续的使用了,唯一剩下的工作就是注册了, 也就是: ...

  9. uc/os 任务删除

    问题描述:     uc/os 任务删除 问题解决: uc/os任务删除流程图 具体代码 注:     如上是关中断,以及取消优先级对应的就绪标志 关中断代码为: 取消就绪标志,实际上是将就绪表中指定 ...

  10. myeclipse报jar包missing

    一.问题描述 从版本库中check out项目后,发现项目有“感叹号”,且pom.xml文件有红色的“差号”.如下图: 在error window里可以看到missing jar包的提示,如下: 打开 ...