【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
2015-07-05 17:18
447 查看
解法一:递归
int maxDepth(TreeNode* root) { if (root == NULL) return 0; int max_left_Depth = maxDepth(root->left); int max_right_Depth = maxDepth(root->right); return max(max_left_Depth, max_right_Depth) + 1; }
解法二:BFS
int maxDepth(TreeNode* root) { if (root == NULL) return 0; queue<TreeNode*> node_queue; node_queue.push(root); int count = 0; while (!node_queue.empty()) { count++; int len = node_queue.size(); for (int i = 0; i < len; ++i) { TreeNode *node = node_queue.front(); node_queue.pop(); if (node->left) node_queue.push(node->left); if (node->right) node_queue.push(node->right); } } return count; }
相关文章推荐
- TVP5150 PAL/NTSC ccd cmos 模拟摄像头视频 转换 VGA输出
- 关于Cache的计算
- 序列化
- 马尔可夫过程(Markov Process)和马尔可夫链
- MySQL存储过程
- Cache
- IG_V3的基本使用教程
- Java学习小心得
- 【Soft-Margin Support Vector Machine】林轩田机器学习技术
- Android开发——为移动的Paint元素指定图片的方法
- Activity动态添加Fragment时遇到的问题
- AVL树原理思想及其实现
- JQuery添加CSS样式
- Linux 套接字编程——帮你捋一捋
- 黑马day13 条件查询
- DedeAMPZ配置php的curl扩展
- 单链表翻转
- jdbc、事务(Transaction)、批处理 回顾
- 开始研究tigase和android客户端的实现
- SWT1-Üb4 Zusammenfassung