1. 程式人生 > >LeetCode-111-Minimum Depth of Binary Tree(二叉樹的最短路徑)

LeetCode-111-Minimum Depth of Binary Tree(二叉樹的最短路徑)

Q:

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.

Analysis:

此題要求返回二叉樹根結點到葉子結點最短的距離。

可以採用遞迴的方法:

Code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution { public int minDepth(TreeNode root) { if(root==null){ return 0; } if (root.left == null && root.right == null) { return 1; } if (root.left == null) { return minDepth(root.right) + 1; } if
(root.right == null) { return minDepth(root.left) + 1; } return Math.min(minDepth(root.left), minDepth(root.right)) + 1; } }

相關推薦

LeetCode-111-Minimum Depth of Binary Tree(路徑)

Q: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root no

[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 lea

LeetCode 104. Maximum Depth of Binary Tree (大深度)

原題 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

LeetCode 104.Maximum Depth of Binary Tree (大深度)

題目描述: 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 /

LeetCode 100. Maximum Depth of Binary Tree 大深度

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeN

LeetCode 111. 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

LeetCode-111. Minimum Depth of Binary Tree

https://leetcode.com/problems/minimum-depth-of-binary-tree/description/ Given a binary tree, find its minimum depth. The minimum depth is the numb

leetcode: 111. Minimum Depth of Binary Tree

Difficulty Easy. Problem Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path fr

LeetCode--111. Minimum Depth of Binary Tree

題目連結:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 求樹的最小深度,也就是說:沿著樹的路徑從根節點到最近葉節點的距離。 需要一個佇列進行層次遍歷(BFS,對層次進行計數),在某一層找到葉節點時返回。 class

[LeetCode] Maximum Depth of Binary Tree 大深度

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

Maximum Depth of Binary Tree-大深度

code clas etc dep 是否 兩種 depth In .com 求二叉樹的最大深度,是常見的一種二叉樹算法問題,主要解決辦法有兩種,一種是使用遞歸求解,另一種是非遞歸方式求解。這裏給出遞歸求解方法。遞歸方法無需判斷左右子樹是否為空。 問題來源於https://

111Minimum Depth of Binary Tree小深度

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度  2

[CareerCup] 4.4 Create List at Each Depth of Binary Tree 的各層建立連結串列

4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll ha

LeetCode-面試演算法經典-Java實現】【111-Minimum Depth of Binary Tree小深度)】

原題   Given a binary tree, find its minimum depth.   The minimum depth is the number of node

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

leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree

treenode right left leetcode oot tco binary 判斷 root 104: class Solution { public: int maxDepth(TreeNode* root) { if(roo

111. Minimum Depth of Binary Tree

查找 minimum rgs sys static http htm leet pos 原題鏈接:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/ 簡單級別的題目就是不咋滴難哦:

LeetCode 112 Minimum Depth of Binary Tree

bin scrip etc turn clas question roo urn 題目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along

111.minimum depth of binary tree(E)

problems div imu target short rom class ear clas 111. minimum depth of binary tree 111. minimum depth of binary tree Given a binary tr

111. Minimum Depth of Binary Tree(python+cpp)

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