-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFindBottomLeftTreeValue.java
More file actions
38 lines (32 loc) · 906 Bytes
/
FindBottomLeftTreeValue.java
File metadata and controls
38 lines (32 loc) · 906 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
33
34
35
36
37
38
package com.dbc.code;
public class FindBottomLeftTreeValue {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
private int res = 0;
private int resDeep = 0;
private void dfs(TreeNode node, int deep) {
if (node.left == null && node.right == null && deep > this.resDeep) {
this.res = node.val;
this.resDeep = deep;
}
if (node.left != null) dfs(node.left, deep + 1);
if (node.right != null) dfs(node.right, deep + 1);
}
public int findBottomLeftValue(TreeNode root) {
dfs(root, 1);
return this.res;
}
}