## Title description

Enter a binary tree and find the depth of the tree. The nodes (including roots and leaf nodes) passing through from the root node to the leaf node in turn form a path of the tree, and the length of the longest path is the depth of the tree.

## Problem solving ideas

Tree's review portal ->
As the name suggests, find its depth, which is the distance from the root node to one of the farthest leaf nodes. And this farthest distance does not appear in the left subtree or the right subtree, so it can be traversed directly.

## Code

``````/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;

}

}
*/
public class Solution {

public int TreeDepth(TreeNode root) {

if(root==null) return 0;
//从左子树进行遍历
int leftNum = TreeDepth(root.left);
//遍历右子树
int rightNum = TreeDepth(root.right);
return Math.max(leftNum,rightNum)+1;
}
}
``````

### Guess you like

Origin blog.csdn.net/H1517043456/article/details/107419665
Recommended
Ranking
Daily