LeetCode Maximum Depth of Binary Tree
来源:互联网 发布:犀牛软件初级教程 编辑:程序博客网 时间:2024/06/06 02:45
/** * 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 { vector<TreeNode*>low; vector<TreeNode*>high; int level;public: int maxDepth(TreeNode* root) { level=0; if(root==NULL) return 0; high.push_back(root); low.clear(); while(!high.empty()){ level++; for(int i=0;i<high.size();i++){ if(high[i]->left) low.push_back(high[i]->left); if(high[i]->right) low.push_back(high[i]->right); } high=low; low.clear(); } return level; }};
递归方式:
class Solution {public: int maxDepth(TreeNode* root) { if(root==NULL) return 0; return 1+max(maxDepth(root->left),maxDepth(root->right)); }};
0 0
- Leetcode - Tree - Maximum Depth of Binary Tree
- [leetcode][tree] Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- [Leetcode] Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- Leetcode 104 Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- Leetcode: Maximum Depth of Binary Tree
- LeetCode-Maximum Depth of Binary Tree
- [leetcode] Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- LeetCode | Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- 数据结构(Java)——查找和排序(1)
- 面试时是该诚实作答还是该说谎?
- 坚持
- 显示Mac下的隐藏文件
- 15个jQuery小技巧
- LeetCode Maximum Depth of Binary Tree
- 黑马程序员-Java的设计模式
- warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
- Linux 常用命令
- iOS APP archive打包和打包成Ipa
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- C# 中使用readonly关键字和const的区别
- hive执行命令Specified key was too long; max key length is 767 bytes 问题
- git 将本地项目上传到github