leetcode-104-Maximum Depth of Binary Tree
来源:互联网 发布:苹果电脑影视制作软件 编辑:程序博客网 时间:2024/05/22 01:28
Maximum Depth of Binary Tree
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.
/** * 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 n=0; int maxDepth(TreeNode* root) { // 递归 if(root==NULL) return 0; //为空则返回 0 return 1+max(maxDepth(root->left),maxDepth(root->right)); //不为空 则返回 左右子树的最大深度 加上1 } int maxDepth(TreeNode* root) { // 非递归 if(!root) return 0; int num=0; // 记录 最大深度 int count_=0; // 每一层的节点数 queue<TreeNode*>q; q.push(root); count_=1; while(!q.empty()){ TreeNode* x=q.front(); q.pop(); count_--; if(x->left) q.push(x->left); if(x->right) q.push(x->right); if(count_==0){ // count_==0 此层的节点访问完 num++ count_=q.size(); num++; } } return num; }};
1 0
- Leetcode 104 Maximum Depth of Binary Tree
- LeetCode(104)Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- LeetCode: Maximum Depth of Binary Tree [104]
- Leetcode(104)Maximum Depth of Binary Tree
- [leetcode 104] Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- leetCode #104 Maximum Depth of Binary Tree
- LeetCode 104: Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- leetcode || 104、Maximum Depth of Binary Tree
- LeetCode 104: Maximum Depth of Binary Tree
- leetcode[104]:Maximum Depth of Binary Tree
- Leetcode[104]-Maximum Depth of Binary Tree
- Leetcode---(104)Maximum Depth of Binary Tree
- leetcode-104-Maximum Depth of Binary Tree
- Leetcode #104 Maximum Depth of Binary Tree
- leetcode 104: Maximum Depth of Binary Tree
- 对常用的安全性测试的总结(以后会陆续更新)_Phoenix-晶
- hibernate缓存的学习
- 剑指offer--旋转数组的最小数字
- vsftpd
- std::vector的几种遍历方式比较
- leetcode-104-Maximum Depth of Binary Tree
- C语言回顾(三、选择循环,数组及Fibonacci)——iOS开发基础
- 2015062002 - 赵云
- POJ 3468 线段树
- Spring整合JMS(一)——基于ActiveMQ实现
- POJ 1046 Color Me Less(水~)
- 小白学c--1 配置开发环境
- leetcode--Find Minimum in Rotated Sorted Array
- PHP实验图片验证码