[LeetCode]Maximum Depth of Binary Tree
2015-07-22 13:58
375 查看
简洁都是相对了,看看我第二遍解此题的java代码
-----------------------第一遍解题思路------------
解题思路:
其实这题很简单,就是深度搜索,每个node都返回自己的depth即可。
但是由于这次写dfs代码的时候,写的更加简练,所以我决定记录下来
public class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } }
-----------------------第一遍解题思路------------
解题思路:
其实这题很简单,就是深度搜索,每个node都返回自己的depth即可。
但是由于这次写dfs代码的时候,写的更加简练,所以我决定记录下来
/** * 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) { return dfs(root); } int dfs(TreeNode *root){ if (root == NULL){ return 0; } return max(dfs(root->left), dfs(root->right)) + 1; } };
相关文章推荐
- 2015多校赛第一场
- WNDCLASS结构中cbWndExtra数据成员的作用
- 如何在页面打开后根据条件让EditorGridPanel的某一行的复选框默认选中?
- java List.subList()方法类似分页分批处理数据
- html from嵌套from
- 夏令营第一期——【一】迎接初升朝阳的序幕
- Linux netlink
- LeetCode Bitwise AND of Numbers Range
- Android SDK在线更新镜像服务器
- HDOJ4552怪盗基德的挑战书
- 关于self.与_的区别
- UML软件建模学习与开发基本概念汇总
- Web前端开发工程师--面试要求
- 样式表写法
- Java通过XML Schema校验XML
- AngularJS2.0 教程系列(一)
- ubuntu下查看windows文件乱码
- 【算法】对一个20GB大的文件排序
- 一、nginx介绍及安装
- linux 下tar 打包分割文件和解压文件