LeetCode - Medium - 1161. Maximum Level Sum of a Binary Tree

﹏ヽ暗。殇╰゛Y 2022-10-10 01:27 172阅读 0赞

Topic

  • Tree
  • Breadth-First Search

Description

https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/

Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.

Return the smallest level x such that the sum of all the values of nodes at level x is maximal.

Example 1:

66387ca3c7fe73a5d0b4efaffb74e973.png

  1. Input: root = [1,7,0,7,-8,null,null]
  2. Output: 2
  3. Explanation:
  4. Level 1 sum = 1.
  5. Level 2 sum = 7 + 0 = 7.
  6. Level 3 sum = 7 + -8 = -1.
  7. So we return the level with the maximum sum which is level 2.

Example 2:

  1. Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
  2. Output: 2

Constraints:

  • The number of nodes in the tree is in the range [ 1 , 1 0 4 ] [1, 10^4] [1,104].
  • − 1 0 5 < = N o d e . v a l < = 1 0 5 -10^5 <= Node.val <= 10^5 −105<=Node.val<=105

Analysis

方法一:BFS

方法二:DFS

Submission

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class MaximumLevelSumOfABinaryTree {
  6. //方法一:BFS
  7. public int maxLevelSum(TreeNode root) {
  8. LinkedList<TreeNode> queue = new LinkedList<>();
  9. queue.offer(root);
  10. int level = 0, minLevel = 0, maxSum = Integer.MIN_VALUE;
  11. while(!queue.isEmpty()) {
  12. level++;
  13. int sum = 0;
  14. for(int size = queue.size(); size > 0; size--) {
  15. TreeNode node = queue.poll();
  16. sum += node.val;
  17. if(node.left != null)
  18. queue.offer(node.left);
  19. if(node.right != null)
  20. queue.offer(node.right);
  21. }
  22. if(sum > maxSum) {
  23. minLevel = level;
  24. maxSum = sum;
  25. }
  26. }
  27. return minLevel;
  28. }
  29. //方法一:DFS
  30. public int maxLevelSum2(TreeNode root) {
  31. List<Integer> tmp = new ArrayList<>();
  32. int minLevel = 0, maxSum = Integer.MIN_VALUE;
  33. maxLevelSum2(root, 0, tmp);
  34. for(int i = 0; i < tmp.size(); i++) {
  35. int sum = tmp.get(i);
  36. if(sum > maxSum) {
  37. minLevel = i + 1;
  38. maxSum = sum;
  39. }
  40. }
  41. return minLevel;
  42. }
  43. private void maxLevelSum2(TreeNode node, int level, List<Integer> tmp) {
  44. if(node == null) return;
  45. if(tmp.size() == level) {
  46. tmp.add(node.val);
  47. }else {
  48. int sum = tmp.get(level) + node.val;
  49. tmp.set(level, sum);
  50. }
  51. maxLevelSum2(node.left, level + 1, tmp);
  52. maxLevelSum2(node.right, level + 1, tmp);
  53. }
  54. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class MaximumLevelSumOfABinaryTreeTest {
  6. @Test
  7. public void test() {
  8. MaximumLevelSumOfABinaryTree mObj = new MaximumLevelSumOfABinaryTree();
  9. TreeNode root1 = BinaryTree.of(1,7,0,7,-8,null,null);
  10. assertEquals(2, mObj.maxLevelSum(root1));
  11. TreeNode root2 = BinaryTree.of(989,null,10250,98693,-89388,null,null,null,-32127);
  12. assertEquals(2, mObj.maxLevelSum(root2));
  13. TreeNode root3 = BinaryTree.of(-100,-200,-300,-20,-5,-10,null);
  14. assertEquals(3, mObj.maxLevelSum(root3));
  15. }
  16. @Test
  17. public void test2() {
  18. MaximumLevelSumOfABinaryTree mObj = new MaximumLevelSumOfABinaryTree();
  19. TreeNode root1 = BinaryTree.of(1,7,0,7,-8,null,null);
  20. assertEquals(2, mObj.maxLevelSum2(root1));
  21. TreeNode root2 = BinaryTree.of(989,null,10250,98693,-89388,null,null,null,-32127);
  22. assertEquals(2, mObj.maxLevelSum2(root2));
  23. TreeNode root3 = BinaryTree.of(-100,-200,-300,-20,-5,-10,null);
  24. assertEquals(3, mObj.maxLevelSum2(root3));
  25. }
  26. }

发表评论

表情:
评论列表 (有 0 条评论,172人围观)

还没有评论,来说两句吧...

相关阅读