[LeetCode-interview algorithm classic-Java implementation] [104-Maximum Depth of Binary Tree (Maximum Depth of Binary Tree)], leetcode -- java
[104-Maximum Depth of Binary Tree (Maximum Depth of a Binary Tree )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
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.
Theme
Calculate the maximum depth of a tree.
Solutions
Recursive solution and recursive formula
F (n) = 0; n = null,
F (n) = 1 + max (f (n left), f (n right ))
Code Implementation
Tree node class
public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
Algorithm Implementation class
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 left = maxDepth(root.left); int right = maxDepth(root.right); return 1 + (left > right ? left : right); } }}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47354355]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.