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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永福县| 张北县| 民权县| 岳普湖县| 华阴市| 绿春县| 贡嘎县| 海淀区| 融水| 东光县| 佛坪县| 青浦区| 花垣县| 孝义市| 葫芦岛市| 江门市| 井冈山市| 聂拉木县| 西平县| 赤壁市| 连江县| 格尔木市| 甘孜县| 万盛区| 宁德市| 固镇县| 女性| 兴化市| 柯坪县| 十堰市| 屏东市| 陇西县| 九台市| 胶南市| 黄山市| 额尔古纳市| 杂多县| 罗平县| 九寨沟县| 潜江市| 南充市|