104. Maximum Depth of Binary Tree
来源:互联网 发布:美国艾绿床垫 知乎 编辑:程序博客网 时间:2024/06/06 01:10
遍历所有可能的情况, 到了叶节点 则更新最大深度
/** * 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 {private: int maxdepth=0; void traverse(TreeNode *root,int prelevel) { if(root) { prelevel++; if(!root->left&&!root->right) { if(prelevel>maxdepth) { maxdepth=prelevel; } } else if(root->left&&root->right) { traverse(root->left,prelevel); traverse(root->right,prelevel); } else if(root->left) traverse(root->left,prelevel); else traverse(root->right,prelevel); } }public: int maxDepth(TreeNode* root) { traverse(root,0); return maxdepth; } };
0 0
- [LeetCode]104.Maximum Depth of Binary Tree
- 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
- 104.Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 104.Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 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
- 在windows 8.1上升级Chrome浏览器到最新版
- Android面试必过——Android常见的问题
- HDU 1233 还是畅通工程
- Android官方提供的支持不同屏幕大小的全部方法
- iOS开发大神必备的Xcode插件
- 104. Maximum Depth of Binary Tree
- 通过IMAP方式迁移U-Mail邮件到Exchange 2013之Exchange 2007 升级到Exchange 2013!
- js的String变量的用法,对jquery的append的一种替代方式
- Elasticsearch入门教程之一--基本概念
- 【leetcode】Palindrome Pairs
- jquery ajax 中的dataType
- SVN-- 当工程中存在 .a 文件时更新svn后会报错(解决如下)
- android 使用Json传输Timestamp格式数据时,如何实现?
- Unity 官方案例 打砖块(二)