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

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

Maximum Depth of Binary Tree

2019-11-08 20:16:52
字體:
來源:轉載
供稿:網友

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.

My code of C++, Depth-first-search and Breadth-first-searchDepth-first-search======Only one line code.int maxDepth(TreeNode *root){    return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;}Breadth-first-search======Calculate the count of the last level.int maxDepth(TreeNode *root){    if(root == NULL)        return 0;        int res = 0;    queue<TreeNode *> q;    q.push(root);    while(!q.empty())    {        ++ res;        for(int i = 0, n = q.size(); i < n; ++ i)        {            TreeNode *p = q.front();            q.pop();                        if(p -> left != NULL)                q.push(p -> left);            if(p -> right != NULL)                q.push(p -> right);        }    }        return res;}
上一篇:Groovy多方法

下一篇:Find the Difference

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五寨县| 利津县| 湟中县| 巍山| 特克斯县| 桂东县| 荔波县| 喀什市| 浪卡子县| 蒙自县| 北辰区| 盘山县| 女性| 灌阳县| 肥西县| 观塘区| 八宿县| 连江县| 莫力| 泰安市| 海原县| 青田县| 临澧县| 乌兰察布市| 靖边县| 镇巴县| 堆龙德庆县| 兰州市| 雅江县| 酒泉市| 根河市| 锡林郭勒盟| 尚志市| 准格尔旗| 瑞安市| 微博| 栖霞市| 攀枝花市| 沭阳县| 安陆市| 镇赉县|