/**
* Definition for a binary tree node.
* 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;
* }
* }
*/
class Solution {
public Map<Integer, Integer> map;
public int max;
public int maxLevelSum(TreeNode root) {
map = new HashMap<>();
max = 0;
count(root, 1);
int maxValue = Integer.MIN_VALUE;
int maxIndex = 0;
for(int i = 1; i <= max; i++) {
if (maxValue < map.get(i)) {
maxValue = map.get(i);
maxIndex = i;
}
}
return maxIndex;
}
public void count(TreeNode node, int level) {
if (node == null) {
return;
}
if (max < level) {
max = level;
}
map.put(level, map.getOrDefault(level, 0) + node.val);
count(node.left, level + 1);
count(node.right, level + 1);
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 216. Combination Sum III (0) | 2024.06.15 |
---|---|
[leetcode] 994. Rotting Oranges (0) | 2024.06.14 |
[leetcode] 1448. Count Good Nodes in Binary Tree (0) | 2024.06.14 |
[leetcode] 872. Left-Similar Leafs (0) | 2024.06.11 |
[leetcode] 443. String Compression (0) | 2024.06.11 |