1161. 最大层内元素和
小于 1 分钟
1161. 最大层内元素和中等
解题思路:
使用广度优先搜索 bfs ,记录每行的最大值与行号
class Solution {
int level = 0;
int res = -1;
int max = -65535;
public int maxLevelSum(TreeNode root) {
List<TreeNode> list = new ArrayList<>();
list.add(root);
while ((list = bfs(list)).size() != 0);
return res;
}
public List<TreeNode> bfs(List<TreeNode> nodes){
level++;
int sum = 0;
List<TreeNode> list = new ArrayList<TreeNode>();
for (int i = 0; i < nodes.size(); i++) {
TreeNode node = nodes.get(i);
sum += node.val;
if(node.left != null) list.add(node.left);
if(node.right != null) list.add(node.right);
}
if(sum > max) {res = level;max = sum;}
return list;
}
}
Powered by Waline v2.15.5