【leetcode刷题笔记】Maximum Depth of Binary Tree
2014-07-17 16:57
429 查看
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.
题解:递归,树的高度 = max(左子树高度,右子树高度)+1;
代码如下:
被虐了一天,瞬间好受多了:-)
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
题解:递归,树的高度 = max(左子树高度,右子树高度)+1;
代码如下:
/** * 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; } }
被虐了一天,瞬间好受多了:-)
相关文章推荐
- 【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归
- LeetCode小白菜笔记[19]: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] 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题目笔记(一) -- Minimum Depth Of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- [Leetcode 8] 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 with Python】 Maximum Depth of Binary Tree
- LeetCode: Maximum Depth of Binary Tree
- leetcode -- Maximum Depth of Binary Tree