-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1123.java
More file actions
71 lines (62 loc) · 2.55 KB
/
LeetCode1123.java
File metadata and controls
71 lines (62 loc) · 2.55 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import util.TreeNode;
public class LeetCode1123 {
public static void main(String[] args) {
// 输入:root = [3,5,1,6,2,0,8,null,null,7,4]
// 输出:[2,7,4]
System.out.println(new Solution1123()
.lcaDeepestLeaves(TreeNode.buildTree(new Integer[] { 3, 5, 1, 6, 2, 0, 8,
null, null, 7, 4 })));
// 输入:root = [1]
// 输出:[1]
System.out.println(new Solution1123()
.lcaDeepestLeaves(TreeNode.buildTree(new Integer[] { 1 })));
// 输入:root = [0,1,3,null,2]
// 输出:[2]
System.out.println(new Solution1123()
.lcaDeepestLeaves(TreeNode.buildTree(new Integer[] { 0, 1, 3, null, 2 })));
// 输入:root = [1,2,null,3,4,null,null,5]
// 输出:[5]
System.out.println(new Solution1123()
.lcaDeepestLeaves(TreeNode.buildTree(new Integer[] { 1, 2, null, 3, 4, null, null, 5 })));
}
}
class Solution1123 {
TreeNode resNode;
int resSelfDepth;
int resChildDepth = 0;
public TreeNode lcaDeepestLeaves(TreeNode root) {
resNode = null;
preorder(root, 0);
return resNode;
}
public int preorder(TreeNode root, int currentDepth) {
if (root == null) {
return 0;
}
// System.out.println(root.val);
int leftTreeHeight = preorder(root.left, currentDepth + 1);
int rightTreeHeight = preorder(root.right, currentDepth + 1);
int currentHeight = Math.max(leftTreeHeight, rightTreeHeight) + 1;
// System.out.println(String.format("resSelfDepth: %d; resChildDepth:
// %d;resNodeValue: %d", resSelfDepth,
// resChildDepth, resNode.val));
// System.out
// .println(String.format("Value: %d; Depth: %d; Height: %d; leftHeight:
// %d;rightHeight: %d", root.val,
// currentDepth, currentHeight, leftTreeHeight, rightTreeHeight));
if (leftTreeHeight == rightTreeHeight) {
// 两子树要一样高,如果不一样高,说明一定存在更深的节点
if (resChildDepth < currentHeight + currentDepth) {
resChildDepth = currentHeight + currentDepth;
resSelfDepth = currentDepth;
resNode = root;
} else if (resChildDepth == currentHeight + currentDepth) {
if (currentDepth < resSelfDepth) {
resSelfDepth = currentDepth;
resNode = root;
}
}
}
return currentHeight;
}
}