|
| 1 | +/** |
| 2 | +1161. Maximum Level Sum of a Binary Tree |
| 3 | +Solved |
| 4 | +Medium |
| 5 | +Topics |
| 6 | +Companies |
| 7 | +Hint |
| 8 | +Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. |
| 9 | +
|
| 10 | +Return the smallest level x such that the sum of all the values of nodes at level x is maximal. |
| 11 | + **/ |
| 12 | + |
| 13 | +// Using Arrays |
| 14 | +/** |
| 15 | + * Definition for a binary tree node. |
| 16 | + * public class TreeNode { |
| 17 | + * int val; |
| 18 | + * TreeNode left; |
| 19 | + * TreeNode right; |
| 20 | + * TreeNode() {} |
| 21 | + * TreeNode(int val) { this.val = val; } |
| 22 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 23 | + * this.val = val; |
| 24 | + * this.left = left; |
| 25 | + * this.right = right; |
| 26 | + * } |
| 27 | + * } |
| 28 | + */ |
| 29 | +class Solution { |
| 30 | + int maxSum; |
| 31 | + int levels; |
| 32 | + int[] arrSum = new int[10001]; |
| 33 | + public int maxLevelSum(TreeNode root) { |
| 34 | + maxSum=Integer.MIN_VALUE; |
| 35 | + levels=1; |
| 36 | + int ret=0; |
| 37 | + findLevelSum(root,levels); |
| 38 | + System.out.println(levels); |
| 39 | + for(int i=1;i<=levels;i++){ |
| 40 | + if(arrSum[i]>maxSum){ |
| 41 | + maxSum=arrSum[i]; |
| 42 | + ret=i; |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + return ret; |
| 48 | + } |
| 49 | + |
| 50 | + public void findLevelSum(TreeNode root,int level){ |
| 51 | + if(root==null) return; |
| 52 | + |
| 53 | + findLevelSum(root.left,level+1); |
| 54 | + findLevelSum(root.right,level+1); |
| 55 | + arrSum[level]+=root.val; |
| 56 | + levels=Math.max(levels,level); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +//Using Map |
| 63 | +/** |
| 64 | + * Definition for a binary tree node. |
| 65 | + * public class TreeNode { |
| 66 | + * int val; |
| 67 | + * TreeNode left; |
| 68 | + * TreeNode right; |
| 69 | + * TreeNode() {} |
| 70 | + * TreeNode(int val) { this.val = val; } |
| 71 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 72 | + * this.val = val; |
| 73 | + * this.left = left; |
| 74 | + * this.right = right; |
| 75 | + * } |
| 76 | + * } |
| 77 | + */ |
| 78 | +class Solution { |
| 79 | + int maxSum; |
| 80 | + int levels; |
| 81 | + Map<Integer,Integer> m = new HashMap<>(); |
| 82 | + public int maxLevelSum(TreeNode root) { |
| 83 | + maxSum=Integer.MIN_VALUE; |
| 84 | + levels=1; |
| 85 | + findLevelSum(root,levels); |
| 86 | + |
| 87 | + for(Map.Entry<Integer,Integer> ma :m.entrySet()){ |
| 88 | + if(ma.getValue()>maxSum){ |
| 89 | + maxSum=ma.getValue(); |
| 90 | + levels=ma.getKey(); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + return levels; |
| 95 | + } |
| 96 | + |
| 97 | + public void findLevelSum(TreeNode root,int level){ |
| 98 | + if(root==null) return; |
| 99 | + |
| 100 | + m.put(level,m.getOrDefault(level,0)+root.val); |
| 101 | + |
| 102 | + findLevelSum(root.left,level+1); |
| 103 | + findLevelSum(root.right,level+1); |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments