LeetCode: 104. Maximum Depth of Binary Tree
来源:互联网 发布:彩票缩水过滤软件下载 编辑:程序博客网 时间:2024/06/16 00:00
LeetCode: 104. Maximum Depth of Binary Tree
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.
自己的答案:
/** * Definition for a binary tree node. * 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; }}
最快的答案:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int max = 0; public int maxDepth(TreeNode root) { if (root == null) return 0; getDepth(root, 0); return max; } private void getDepth(TreeNode root, int count) { if (root == null) return; count++; if (root.left == null && root.right == null) { max = Math.max(count, max); } getDepth(root.left, count); getDepth(root.right, count); }}
Two Java Iterative solution DFS and BFS
阅读全文
0 0
- [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
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- 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
- Linux 常用命令
- 【openjudge】迷宫
- javaweb 每天定时执行任务
- 关注程序性能(2) jstack, jmap, jvisualvm
- springMVC 注解及配置文件详解
- LeetCode: 104. Maximum Depth of Binary Tree
- The R Formula Method: The Bad Parts
- Python操作mysql数据库
- PHP使用Curl扩展Post多维数组的例子
- [LeetCode] Binary Search Tree Iterator
- 一种基于FreeRTOS的CPU使用率测算方法及原理介绍
- QQ启动时:Initialization failure:0x0000000C 解决方案
- nginx负载均衡简单配置
- CTS Verifier:Widget Framework Test如何测试?