国产探花免费观看_亚洲丰满少妇自慰呻吟_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;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华安县| 庆阳市| 中江县| 昌邑市| 酒泉市| 铁岭市| 广元市| 图木舒克市| 揭东县| 东丰县| 翁源县| 河池市| 景德镇市| 禹城市| 砀山县| 台南县| 彭州市| 洪泽县| 巨鹿县| 泾阳县| 桂东县| 武安市| 锦州市| 奉新县| 信阳市| 崇文区| 建湖县| 宁武县| 阿坝| 盐源县| 从江县| 辰溪县| 台安县| 体育| 墨竹工卡县| 塔河县| 修水县| 重庆市| 奇台县| 石河子市| 华安县|