LeetCode 刷题之二:寻找二叉树的最大深度
2014-12-12 23:17
369 查看
题目为:
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.
阶梯思路:对于这种题目最简单的方法就是递归操作了
代码为:
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) { if(root == null) return 0; else if(root.left == null && root.right == null) return 1; else { int leftDepth= maxDepth(root.left); int rightDepth = maxDepth(root.right); if(leftDepth > rightDepth) return leftDepth +1; else return rightDepth +1; } } }
相关文章推荐
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- LeetCode—二叉树的最大最小深度
- 二叉树的最大深度(leetcode-104)
- [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
- Leetcode 104.Maximum Depth of Binary Tree (二叉树的最大深度)
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- LeetCode:寻找二叉树的最小深度
- leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- 【leetcode Java】二叉树的递归遍历以及最大深度的求解(Java)
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度