leetcode || 104、Maximum Depth of Binary Tree
来源:互联网 发布:速卖通seo 编辑:程序博客网 时间:2024/05/21 17:44
problem:
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.
Tree Depth-first Search题意:输出二叉树的深度
thinking:
深度优先搜索,记录最大深度
code:
/** * Definition for binary tree * 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==NULL) return 0; int max_dep=0; dfs(0,max_dep,root); return max_dep; }protected: void dfs(int dep,int &max_dep,TreeNode *node) { if(node==NULL) return; dep++; max_dep = max(max_dep,dep); if(node->left!=NULL) dfs(dep,max_dep,node->left); if(node->right!=NULL) dfs(dep,max_dep,node->right); }};
0 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
- mysql修改字符集
- 使用 vmware+windbg 分析调试 windows 7 内核
- JavaScript跨域方法学习总结
- listview的item中有checkbox,仍要整个item响应点击或长按事件
- SQL Server 将查询结果导出插入(insert)语句的简单方式
- leetcode || 104、Maximum Depth of Binary Tree
- IOS数据持久化之对象归档
- Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码
- boost库asio详解3——io_service作为work pool
- iOS应用开发及Xcode开发工具使用总结
- Nginx Tomcat 负载均衡配置
- linux 下ps指令详解
- Altium Designer 覆铜时过孔连接形式的设置——只将过孔连接设置为 Direct Connect
- Java 多线程实现接口Runnable和继承Thread区别