Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
来源:互联网 发布:网贷记账软件 编辑:程序博客网 时间:2024/06/15 23:52
Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
Tag: Tree, Depth-first Search
Difficulty: Easy
/*104. Maximum Depth of Binary Tree (Easy)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 maxDepth(TreeNode* root) { if (!root) return 0; int res = 0; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int _size = q.size(); res++; for (int i = 0; i < _size; i++) { TreeNode *temp = q.front(); q.pop(); if (temp -> left) q.push(temp -> left); if (temp -> right) q.push(temp -> right); } } return res; } }; class Solution { public: int maxDepth(TreeNode* root) { if (!root) return 0; else if (!(root -> left) && !(root -> right)) return 1; int depth_L = maxDepth(root -> left); int depth_R = maxDepth(root -> right); return depth_L > depth_R ? depth_L + 1 : depth_R + 1; } };
0 0
- Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
- 【leetcode】【Easy】【104. Maximum Depth of Binary Tree】【tree】
- Leetcode----104. Maximum Depth of Binary Tree(easy)
- LeetCode解题报告 104. Maximum Depth of Binary Tree [easy]
- 104. Maximum Depth of Binary Tree (Easy)
- Leetcode 111. Minimum Depth of Binary Tree (Easy) (cpp)
- [Leetcode 104, Easy] Minimum (Maximum) Depth of Binary Tree
- 【LeetCode】(104)Maximum Depth of Binary Tree(Easy)
- LeetCode #104 - Maximum Depth of Binary Tree - Easy
- 【Leetcode-easy-104】Maximum Depth of Binary Tree
- LeetCode-Easy刷题(21) Maximum Depth of Binary Tree
- leetcode 日经贴,Cpp code -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
- Mac 下mysql的操作
- gcc工具使用
- android developer tiny share-20160722
- Android 商品倒计时(DigitalClock实现倒计时 )
- uva10803
- Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
- 求两个数的最大公约数与最小公倍数
- 企业征信查询工具--企信宝
- hdu 5744 Keep On Movin(2016 Multi-University Training Contest 2——回文串,思维题)
- Spring Data JPA中的动态查询
- 嵌入式常用算法:异步架构-延时为例
- qt 之 QPushbutton
- python dir()内建函数
- 计算几何(组合计数+向量排序)——Eureka ( HDU 5738 ) ( 2016 Multi-University Training Contest 2 1005 )