forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT55.java
More file actions
32 lines (28 loc) · 860 Bytes
/
Copy pathT55.java
File metadata and controls
32 lines (28 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package books;
/**
* @program JavaBooks
* @description: 二叉树的深度
* @author: mf
* @create: 2019/10/08 09:38
*/
/*
输入一颗二叉树的根节点,求该树的深度。从根节点到叶节点
依次经过的节点(含根、叶节点)形成树的一条路径,最长路径
的长度为树的深度。
*/
public class T55 {
public static void main(String[] args) {
int[] pre = {1, 2, 4, 5, 7, 3, 6};
int[] in = {4, 2, 7, 5, 1, 3, 6};
TreeNode pNode = TreeNode.setBinaryTree(pre, in);
System.out.println(treeDepth(pNode));
}
// 递归
private static int treeDepth(TreeNode pNode) {
if (pNode == null)
return 0;
int pLeft = treeDepth(pNode.left);
int pRight = treeDepth(pNode.right);
return pLeft > pRight ? (pLeft + 1) : (pRight + 1);
}
}