| Title: |
Maximum Depth of Binary Tree |
Pass Rate: |
44.2% |
| Difficulty: |
Simple |
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
Using recursive thinking, traversing the left and right sub-tree, each time to determine the depth of the left and right sub-tree, the return of the deep side is a binary tree, the specific code is as follows:
1 /**2 * Definition for binary tree3 * public class TreeNode {4 * int val;5 * TreeNode left;6 * TreeNode right;7 * TreeNode (int x) {val = x;}8 * }9 */Ten Public classSolution { One Public intmaxDepth (TreeNode root) { A intDepth1=0; - intDepth2=0; - if(root==NULL)return0; the // Left -depth1=maxDepth (root.left); -Depth2=maxDepth (root.right); - if(depth1>depth2) + returnDepth1+1; - Else + returnDepth2+1; A } at}
Leetcode------Maximum Depth of Binary Tree