首页 > 其他 > 详细

Maximum Depth of Binary Tree

时间:2015-04-11 11:37:43      阅读:190      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!