leetcode -Maximum Depth of Binary Tree
来源:互联网 发布:阮一峰 js实现继承 编辑:程序博客网 时间:2024/05/10 10:53
question:
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 Searchanswer:
递归法:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root ==null){
return 0;
}
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return Math.max(left,right)+1;
}
}
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
- GET和POST(ajax request, form submit, 上传文件)
- 【Linux】CentOS安装Chrome浏览器
- 【时间管理】对工作的进度得把控
- IOS UIview设置背景图片以及设置背景色遇到的问题
- hdu 5107 线段树+离散化+归并排序+极角排序
- leetcode -Maximum Depth of Binary Tree
- jdk install_win7环境变量设置
- HTML和CSS
- java基础6
- Linux rpm 命令参数使用详解[介绍和应用]
- 浏览器兼容性汇总
- Oracle启动服务后 报 :ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- SEU寒假训练题解二 A Codeforces 461A
- Android学习笔记 - fragment