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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌市| 禹城市| 梁山县| 阿勒泰市| 丰台区| 高台县| 林州市| 南皮县| 嵩明县| 荣成市| 竹山县| 章丘市| 邳州市| 涡阳县| 阿尔山市| 盘山县| 昌宁县| 米泉市| 陆良县| 琼中| 鄂州市| 岚皋县| 瑞安市| 崇阳县| 西乡县| 玉田县| 文化| 五常市| 铁岭县| 永昌县| 临漳县| 丽江市| 阿拉善右旗| 东城区| 卢氏县| 九寨沟县| 砀山县| 和田县| 宝应县| 临武县| 太原市|