OJ练习26——T111 Minimum Depth of Binary Tree

求从根节点到叶子节点的最短路径。

【思路】

开始想到返回左右子树最短深度,测试发现对于一个根节点带一个孩子节点,其最短路径是2,不是最小深度1,此种特殊情况单独列出。

【my code】

int minDepth(TreeNode *root) {
        if(root==NULL)
            return 0;
        int depthl=minDepth(root->left);
        int depthr=minDepth(root->right);
        if(depthl!=0&&depthr!=0)
            return min(depthl, depthr)+1;
        else
            return max(depthl, depthr)+1;
    }

【结果】

15ms,性能比较靠前。

【其他】

看了网上的,有很多遍历找到叶子节点,累计深度的,看起来比较复杂。

时间: 2024-12-18 05:08:02

OJ练习26——T111 Minimum Depth of Binary Tree的相关文章

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. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/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. 解题思路:分几种情况考虑:1,树为空,

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 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. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

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

Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/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

35: Minimum Depth of Binary Tree

/************************************************************************/        /*       35:     Minimum Depth of Binary Tree                                         */        /***********************************************************************

Leetcode 111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree Total Accepted: 114343 Total Submissions:368035 Difficulty: Easy 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 le

LeetCode My Solution: Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions 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 -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

[Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree

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. 比较左子树和右子树的深度,取小的那个返回上来,并+1. 需要注意的是如果没有左子树或右子树.那么无条件取存在的那一边子树深