Leetcode: Maximum Depth of Binary Tree
来源:互联网 发布:游戏美工外包 编辑:程序博客网 时间:2024/05/04 12:32
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 binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDepth(TreeNode root) {// Start typing your Java solution below// DO NOT write main() functionif(root == null)return 0;elsereturn traversal(root, 1);}public int traversal(TreeNode root, int depth){if(root == null)return Math.max(0, depth);// actually no need to test depth < 0int left = depth; int right = depth;if(root.left != null)left = traversal(root.left, left + 1);if(root.right != null)right = traversal(root.right, right + 1);return Math.max(left, right);}}
/** * 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) {// Start typing your Java solution below// DO NOT write main() functionif(root == null)return 0;int left = maxDepth(root.left) + 1;int right = maxDepth(root.right) + 1;return left > right ? left : right;}}
- 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
- Java--调试--单步调试,断言,单元测试
- cortex-A8 ARM核 opencv程序移植 (2)
- 再看三层
- JAVA中int、String的类型转换
- HardFault_Handler问题查找方法
- Leetcode: Maximum Depth of Binary Tree
- Android平台OpenMax多媒体引擎介绍
- XMLHttpRequest
- 资深idc从业者教你如何选择最合适bgp机房
- android 图片很卡 很慢现象
- 同时显示多个 Notification
- 关于android蓝牙每次开机有时候打不开
- 基于SSH2框架Struts2拦截器的登录验证实现 来自菠萝大象
- Access PostgreSQL with Python