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.

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

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int minDepth(struct TreeNode* root) {
int left, right;
if(root == NULL)
return 0;
else if(root->left == NULL && root->right == NULL)
return 1;
else if(root->left == NULL && root->right != NULL)
return minDepth(root->right) + 1;
else if(root->right == NULL && root->left != NULL)
return minDepth(root->left) + 1;
else
{
left = minDepth(root->left) + 1;
right = minDepth(root->right) + 1;
return ((left > right) ? right : left);
}
}

Maximum & Minimum Depth of Binary Tree的更多相关文章

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

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

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

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  5. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

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

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

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

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

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

  8. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  9. LeetCode: Minimum Depth of Binary Tree 解题报告

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

随机推荐

  1. caffe源代码分析--data_layer.cpp

    dataLayer作为整个网络的输入层, 数据从leveldb中取. leveldb的数据是通过图片转换过来的. 网络建立的时候. datalayer主要是负责设置一些參数,比方batchsize.c ...

  2. Android通过HTTP POST带參訪问asp.net网页

    在看了网络上非常多视频关于android通过HTTP POST或者GET方式訪问网页并获取数据的方法. 自己也copy了一份来測试.并通过C#.NET搭建了一个简单的后台,但发现传參时,依照网上的方式 ...

  3. Mule与其它web应用服务器的区别

    跟JBoss.Tomcat或其它web应用服务器相比,Mule有何不同?虽然他们有一些重要的相同点,不同点可以归结为你想达到的目标是什么.某些种类的应用对于Mule来说比较容易去编写.部署和管理,其它 ...

  4. ORACLE 热备begin backup / end backup

    执行begin backup之后,oracle会把将要备份的数据文件都标记为hot-backup-in-progress,锁定所要备份的datafile header的scn,例如此时scn=100, ...

  5. 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】

    On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you ...

  6. HashMap为什么线程不安全(hash碰撞与扩容导致)

    一直以来都知道HashMap是线程不安全的,但是到底为什么线程不安全,在多线程操作情况下什么时候线程不安全? 让我们先来了解一下HashMap的底层存储结构,HashMap底层是一个Entry数组,一 ...

  7. 编写isNull isArray isFunction的方法

    1.isNull 判断null,需要排除掉undefined和0.''(空串). function isNull(arr){ return !arr&&typeof arr!=='un ...

  8. Win32汇编开始 Hello Asm

    今天开始学习Win32汇编 因为自己很多都是Windows方面 所以 接触一下Win32汇编 . ;.386指令集 .model flat,stdcall ;工作模式 option casemap:n ...

  9. 使用PowerShell管理Windows8应用

    引子(?): 我从消费者预览版开始使用的win8,大概是因为我年龄不大的缘故,我很快熟悉了这个操作系统并习惯了使用windows8的Modern App.我之前使用过一个叫Windows8 Moder ...

  10. 在Windows的Wamp环境下安装Composer

    注意: PHP缺少openssl扩展. 你可能会去屏幕右下角的Wamp的控制台,去加载php的openssl扩展,或者在php.ini中去掉 extension=php_openssl.dll 这一行 ...