104. Maximum Depth of Binary Tree

来源:互联网 发布:白色相簿2去音乐会知乎 编辑:程序博客网 时间:2024/06/01 21:09

题目

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.

题意

找出树的最大深度

分析

使用BFS.

实现

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int maxDepth(TreeNode* root) {        if (!root) return 0;        queue<TreeNode*> q;        q.push(root);        int res = 0;        TreeNode *cur = root;        while (!q.empty()) {            int n = q.size();            while (n--) {                cur = q.front();                if (cur->left) q.push(cur->left);                if (cur->right) q.push(cur->right);                q.pop();            }            res++;        }        return res;    }};
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 左岸春风一语断蓝汇总 夏虫不可语冰下一句 一语成畿 晨语分享每日一条 一语破春风作品 一语不能践 越南语新手一学就会 一语成谶怎么读音 正常语速一分钟多少字 2019左岸春风一语断蓝 左岸春风今日一语断兰 左岸春风今日一语定蓝 君子一诺 一诺千金 一诺孙尚香五杀 洪一诺 刘诺一 一诺千金故事 秦诺一梁晔宸 一诺千金意思 秦诺梁晔宸 一刀流诺手 季布一诺 一诺杀队友 韩娱之允诺一生 千金一诺 洪一诺图片 何一诺 一诺医学 雪诺龙母船震在哪一集 君子一诺番外之孩子上学 一医成婚景诺 一诺终生 斯诺登希望法国批准庇护申请 何一 你诉说爱情邢烈寒邢一诺 一第 第一财经 中国第一财经网 笫一财经 世界第一宠财迷萌宝