-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path513.java
More file actions
61 lines (55 loc) · 1.58 KB
/
513.java
File metadata and controls
61 lines (55 loc) · 1.58 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
__________________________________________________________________________________________________
sample 0 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
int value = 0;
public int findBottomLeftValue(TreeNode root) {
preorder(root, 1);
return value;
}
void preorder(TreeNode root, int depth) {
if (root == null) {
return;
}
if (depth > max) {
max = depth;
value = root.val;
}
preorder(root.left, depth + 1);
preorder(root.right, depth + 1);
}
}
__________________________________________________________________________________________________
sample 37632 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeMap<Integer, TreeNode> map = new TreeMap<>();
public void dfs(TreeNode cur, int depth) {
if (cur == null) return;
if (!map.containsKey(depth)) map.put(depth, cur);
dfs(cur.left, depth+1);
dfs(cur.right, depth+1);
}
public int findBottomLeftValue(TreeNode root) {
dfs(root, 0);
return map.get(map.lastKey()).val;
}
}
__________________________________________________________________________________________________