国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Minimum Depth of Binary Tree

2019-11-15 01:11:50
字體:
來源:轉載
供稿:網友
[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.

這道題個人覺得其實思路和之前那個maximum depth的題思路差不多。那道題能做這道題也就沒問題了。

代碼如下。~

/** * 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;        }        int min=Integer.MAX_VALUE;        Stack<TreeNode> tree=new Stack<TreeNode>();        Stack<Integer> value=new Stack<Integer>();        tree.push(root);        value.push(1);        while(!tree.isEmpty()){            TreeNode temp=tree.pop();            int val=value.pop();            if(temp.left==null&&temp.right==null){                min=Math.min(val,min);            }            if(temp.right!=null){                tree.push(temp.right);                value.push(val+1);            }            if(temp.left!=null){                tree.push(temp.left);                value.push(val+1);            }        }        return min;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淄博市| 当雄县| 肇源县| 尖扎县| 孝昌县| 宝山区| 稻城县| 南靖县| 桃江县| 武隆县| 马尔康县| 浦东新区| 满洲里市| 咸丰县| 黑水县| 成都市| 东丰县| 延安市| 庆安县| 江门市| 兴和县| 横山县| 大同市| 泰顺县| 永宁县| 平陆县| 平远县| 恩平市| 颍上县| 吉水县| 高青县| 雅江县| 当涂县| 榆树市| 广元市| 沁源县| 疏附县| 桐庐县| 彰武县| 涞源县| 无为县|