二叉树最大深度和最小深度

二叉树的最大深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的距离。

如果二叉树为空,则深度为0

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

int maxDepth(TreeNode *root) {
        // write your code (here)
        if(root == NULL)
            return 0;
        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);

        return leftDepth > rightDepth ? (leftDepth + 1) : (rightDepth + 1);
    }

二叉树的最小深度

给定一个二叉树,找出其最小深度。

二叉树的最小深度为根节点到最近叶子节点的距离。

刚开始拿到这道题,就想不是很简单吗,不就是最大深度的代码改为最小就行了吗? 这样做的结果就是有些测试时通不过的。

因为如果出现斜树的情况,也就是说只有左子树,或只有右子树的时候,如果按照刚才说的那个算法来实现的话,得出的答案是0,明显是错误的。

两种实现方法,一种就是计算左子树和右子树深度的时候,判断是否等于0,如果等于0,说明该子树不存在,深度赋值为最大值。

第二种就是判断左子树或右子树是否为空,若左子树为空,则返回右子树的深度,反之返回左子树的深度,如果都不为空,则返回左子树和右子树深度的最小值

int minDepth(TreeNode *root) {
        // write your code here
        if(root == NULL)
            return false;
        if(root->left == NULL && root->right == NULL)
            return 1;

        int leftDepth = minDepth(root->left);
        if(leftDepth == 0)
            leftDepth = INT_MAX;

        int rightDepth = minDepth(root->right);
        if(rightDepth == 0)
            rightDepth = INT_MAX;

        return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1);
    }
int minDepth(TreeNode *root) {
        // write your code here
        if(root == NULL)
            return false;

        if(root->left == NULL) return minDepth(root->right) + 1;
        if(root->right == NULL) return minDepth(root->left) + 1;

        int leftDepth = minDepth(root->left);
        int rightDepth = minDepth(root->right);

        return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1);
    }

(97) Maximum Depth of Binary Tree

http://www.lintcode.com/zh-cn/problem/maximum-depth-of-binary-tree/

(155) Minimum Depth of Binary Tree

http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 08:05:48

二叉树最大深度和最小深度的相关文章

递归的应用--求二叉树最大深度和最小深度

[求最大深度]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. 这里说的最大深度是指最深叶子节点到根节点的路径长度 <span style="font-size:18px;"> public static i

递归求取二叉树最小深度和最大深度

public class Binarytreedept { /*  * 输出二叉树最小深度     * 核心思想:根节点到达最近的叶子节点的路径长度.  * 1.当根为空时,输出0.  * 2.当左子树为空时,输出右子树深度+1.  * 3.当右子树为空时,输出左子树深度+1.  * 4.以上条件都不满足时,输出min(左子树深度,右子树深度)+1.  *   * 输出二叉树最大深度   * 核心思想:根节点到达最远的叶子节点的路径长度.  * 1.如果二叉树为空,则深度为0;  * 2.如果不

LintCode 二叉树的最小深度

给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 样例 给出一棵如下的二叉树: 1 /     \ 2       3 /    \ 4      5 这个二叉树的最小深度为 2 分析:与最大深度有区别 有单孩子的情况考虑 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * th

155 二叉树的最小深度

原题网址:https://www.lintcode.com/problem/minimum-depth-of-binary-tree/description 描述 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题?  是 样例 给出一棵如下的二叉树: 1 /     \ 2       3 /    \ 4      5 这个二叉树的最小深度为 2 标签 二叉树 Depth-first Search(DFS) 思路:可参照二叉树的

求二叉树的最小深度

思路:用递归的方法求解. 输入:二叉树的根节点: 输出:二叉树的最小深度. 最小深度的定义:从根节点到叶子节点的最短路径上的节点数. 算法如下: 将二叉树分为这么几种情况: 传入的根节点为空,返回NULL: 传入根节点不为空,左子树为空,右子树为空,返回最小深度1: 传入根节点不为空,左子树为空,右子树不为空,返回右子树的最小深度+1: 传入根节点不为空,左子树不为空,右子树为空,返回左子树的最小深度+1: 传入根节点不为空,左右子树都不为空,则返回左右子树中最小深度的较小值+1. 代码如下:

【JAVA】【leetcode】【查找二叉树最小深度】

题目:  minimum-depth-of-binary-tree 要求: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. 思路: 两种方法: 第一,使用递归,相当于遍历了整个二叉树,递归返回深度浅的那棵子树的深度. 第二,按层检查

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

[111-Minimum Depth of Binary Tree(二叉树的最小深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大意 给定

LinCode 刷题 之二叉树最小深度

http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/  题目描述信息 二叉树的结构定义如下: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.lef

二叉树的最小深度——广度优先搜索

题目描述: 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 解题思路: 这个题目比较简单. 对于二叉树的问题,首先想到的是采用递归,广度优先搜索. 一个节点一个节点地遍历,直到第一次找到叶子节点为止. 注意编程的细节,代码里面有注释 参考代码:(C++) <span style="font-size:18px;">/** * Definition of TreeNode: * class TreeNode { * public: * int