Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public int maxLevelSum(TreeNode root) {
- if(root == null) return 0;
- List<Integer> sums = new ArrayList<>();
- Queue<TreeNode> q = new LinkedList<>();
- q.offer(root);
- while(!q.isEmpty()){
- int len = q.size();
- int sum = 0;
- while(len-->0){
- TreeNode cur = q.poll();
- sum+=cur.val;
- if(cur.left != null)
- q.offer(cur.left);
- if(cur.right != null)
- q.offer(cur.right);
- }
- sums.add(sum);
- }
- int ret = 0;
- int max = sums.get(0);
- for(int i=1;i<sums.size();i++){
- if(sums.get(i) > max){
- max = sums.get(i);
- ret = i+1;
- }
- }
- return ret;
- }
- }
Add Comment
Please, Sign In to add comment