码迷,mamicode.com
首页 > 其他好文 > 详细

Maximum Depth of Binary Tree

时间:2015-04-11 11:38:05      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

求一棵树的最大深度

思路:广度优先搜索即可

  1. class Solution {
  2. public:
  3. int maxDepth(TreeNode *root) {
  4. int depth = 0;
  5. if (!root)
  6. return depth;
  7. queue<TreeNode*> nodeQue;
  8. nodeQue.push(root);
  9. while (!nodeQue.empty())
  10. {
  11. depth++;
  12. int qSize = nodeQue.size();
  13. for (size_t i = 0; i < qSize; i++)
  14. {
  15. if (nodeQue.front()->left)
  16. nodeQue.push(nodeQue.front()->left);
  17. if (nodeQue.front()->right)
  18. nodeQue.push(nodeQue.front()->right);
  19. nodeQue.pop();
  20. }
  21. }
  22. return depth;
  23. }
  24. };




Maximum Depth of Binary Tree

标签:

原文地址:http://www.cnblogs.com/flyjameschen/p/f7d1f1fbb1edbecffb39ada376fa6f60.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!