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.

解题:

递归就行:根节点为空,返回0;一个子树根节点为空,另一个子树根节点不为空,就返回根节点不为空的子树高度;否则返回两个子树中高度大者加一。

代码:

 /**
* Definition for binary tree
* 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(root == NULL)
return ;
if(root->left != NULL && root->right == NULL)
return maxDepth(root->left)+;
if(root->right != NULL && root->left == NULL)
return maxDepth(root->right)+;
int h_left = maxDepth(root->left);
int h_right = maxDepth(root->right);
return h_left > h_right ? h_left+:h_right+;
}
};

Java版本代码:

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

【leetnode刷题笔记】Maximum Depth of binary tree的更多相关文章

  1. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

  2. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

  3. [LintCode] 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: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 ...

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. 【LeetCode练习题】Maximum Depth of Binary Tree

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

  7. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  8. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

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

  9. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

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

随机推荐

  1. Cent OS安装My Sql

    因为公司的需要,所以就自己学习了一下在Linux上安装MySQL,但是翻查了好多博客,没有特别清楚,自己写下来好好总结一下 一.系统环境 CentOS-6.3-i386-bin-DVD1 二.下载My ...

  2. Asp.Mvc将生成的视图保存为字符串

    public static class ViewExtensions { /// <summary> /// 在控制器内获取指定视图生成后的HTML /// </summary> ...

  3. CoreImage的模糊滤镜

    //1.原始图片 UIImage * image = [UIImage imageNamed:@"1.jpg"]; /****************core image***** ...

  4. WCF实现上传图片功能

    初次学习实现WCF winform程序的通信,主要功能是实现图片的传输. 下面是实现步骤: 第一步: 首先建立一个类库项目TransferPicLib,导入wcf需要的引用System.Service ...

  5. 原 [Android]LIstView的HeaderView

    目录[-] (1)添加HeaderView之后尺寸布局被忽略. (2)添加HeaderView之后导致OnItemClickListener的position移位 (3)LayoutInflater的 ...

  6. MongoDB 常见的查询索引

    常见的查询索引 _id索引         _id 索引是绝大多数集合默认建立的索引.对于每一个插入的数据.MongoDB 会自己主动生成一条唯一的 _id 字段. 1 2 3 4 5 6 7 8 9 ...

  7. C# Html Agility Pack

    using System; using HtmlAgilityPack; using System.IO; using System.Text; using System.Text.RegularEx ...

  8. SpringSecurity---javaconfig:Hello Web Security

    © 版权声明:本文为博主原创文章,转载请注明出处 本文根据官方文档加上自己的理解,仅供参考 官方文档:https://docs.spring.io/spring-security/site/docs/ ...

  9. CSRF--花式绕过Referer技巧

    CSRF遇到Referer绕过的情况,有条件限制,不一定所有的Refere验证就可以绕过 1.Refere为空条件下 解决方案: 利用ftp://,http://,https://,file://,j ...

  10. NFS网络文件系统服务(配置实战)

    NFS网络文件系统服务(实战) NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过TCP/IP网络共享资源.让不同的主机系统(NFS的客户端)可以透明地读写位 ...