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

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

[LeetCode] Maximum Depth of Binary Tree

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

這道題我們借助stack和iteration就可以做出來了。

一個stack<TreeNode>用于node,另一個stack<Integer>用于depth的value。

代碼如下。~

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙田区| 兰西县| 方正县| 牡丹江市| 黑水县| 肇东市| 大悟县| 依兰县| 界首市| 邢台县| 双辽市| 呼伦贝尔市| 海丰县| 新丰县| 上虞市| 阳泉市| 崇礼县| 榆中县| 获嘉县| 朔州市| 教育| 阜南县| 白朗县| 微山县| 遵义市| 互助| 黑河市| 苏尼特左旗| 林西县| 伊春市| 磐安县| 石林| 彰化市| 丹巴县| 兰西县| 来宾市| 元阳县| 泊头市| 青浦区| 肥东县| 镇远县|