LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)
2017-07-02 06:32
399 查看
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 这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量。利用post order 来遍历二叉树,对于每一个点,它的两个children会返回两个depth值,取一个大的, 加上1继续返回。当遍历到最深的level,当一个点node == null的时候,说明走到底了,返回0。
Java Solution:
Runtime beats 15.89%
完成日期:07/01/2017
关键词:Tree
关键点:post order 来遍历树; Math.max 来取一个更大的值(从两个children返回的depth)
/** * 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) { int depth = 0; if(root == null) return depth; depth = Math.max(maxDepth(root.left), maxDepth(root.right)); return depth + 1; } }
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
相关文章推荐
- Leetcode 104.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 662. Maximum Width of Binary Tree 二叉树最大宽度 + 深度优先遍历DFS
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- [LeetCode]104. Maximum Depth of Binary Tree(二叉树最大深度)
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法
- Leetcode 104 Maximum Depth of Binary Tree 二叉树的最大深度
- 【LeetCode】104. Maximum Depth of Binary Tree 求解二叉树最大深度
- 【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- LeetCode----Maximum Depth of Binary Tree 求二叉树最大深度
- [LeetCode]104. Maximum Depth of Binary Tree--二叉树的最大深度
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度 - Easy
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法