LeetCode - Medium - 515. Find Largest Value in Each Tree Row

谁借莪1个温暖的怀抱¢ 2022-10-14 01:22 325阅读 0赞

Topic

  • Tree
  • Depth-first Search
  • Breadth-first Search

Description

https://leetcode.com/problems/find-largest-value-in-each-tree-row/

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

90df2b9d68775c76d1f8cb053b8e3fdc.png

Example 1:

  1. Input: root = [1,3,2,5,3,null,9]
  2. Output: [1,3,9]

Example 2:

  1. Input: root = [1,2,3]
  2. Output: [1,3]

Example 3:

  1. Input: root = [1]
  2. Output: [1]

Example 4:

  1. Input: root = [1,null,2]
  2. Output: [1,2]

Example 5:

  1. Input: root = []
  2. Output: []

Constraints:

  • The number of nodes in the tree will be in the range [ 0 , 1 0 4 ] [0, 10^4] [0,104].
  • − 2 31 < = N o d e . v a l < = 2 31 − 1 -2^{31} <= Node.val <= 2^{31} - 1 −231<=Node.val<=231−1

Analysis

方法一:BFS

方法二:DFS

Submission

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

Test

  1. import static org.junit.Assert.*;
  2. import org.hamcrest.collection.IsEmptyCollection;
  3. import org.hamcrest.collection.IsIterableContainingInOrder;
  4. import org.junit.Test;
  5. import com.lun.util.BinaryTree;
  6. public class FindLargestValueInEachTreeRowTest {
  7. @Test
  8. public void test() {
  9. FindLargestValueInEachTreeRow obj = new FindLargestValueInEachTreeRow();
  10. assertThat(obj.largestValues(BinaryTree.integers2BinaryTree(1, 3, 2, 5, 3, null, 9)),//
  11. IsIterableContainingInOrder.contains(1, 3, 9));
  12. assertThat(obj.largestValues(BinaryTree.integers2BinaryTree(1, 2, 3)),//
  13. IsIterableContainingInOrder.contains(1, 3));
  14. assertThat(obj.largestValues(BinaryTree.integers2BinaryTree(1)),//
  15. IsIterableContainingInOrder.contains(1));
  16. assertThat(obj.largestValues(BinaryTree.integers2BinaryTree(1, null, 2)),//
  17. IsIterableContainingInOrder.contains(1, 2));
  18. assertThat(obj.largestValues(null), IsEmptyCollection.empty());
  19. }
  20. @Test
  21. public void test2() {
  22. FindLargestValueInEachTreeRow obj = new FindLargestValueInEachTreeRow();
  23. assertThat(obj.largestValues2(BinaryTree.integers2BinaryTree(1, 3, 2, 5, 3, null, 9)),//
  24. IsIterableContainingInOrder.contains(1, 3, 9));
  25. assertThat(obj.largestValues2(BinaryTree.integers2BinaryTree(1, 2, 3)),//
  26. IsIterableContainingInOrder.contains(1, 3));
  27. assertThat(obj.largestValues2(BinaryTree.integers2BinaryTree(1)),//
  28. IsIterableContainingInOrder.contains(1));
  29. assertThat(obj.largestValues2(BinaryTree.integers2BinaryTree(1, null, 2)),//
  30. IsIterableContainingInOrder.contains(1, 2));
  31. assertThat(obj.largestValues2(null), IsEmptyCollection.empty());
  32. }
  33. }

发表评论

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

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

相关阅读