LeetCode - Medium - 513. Find Bottom Left Tree Value

朴灿烈づ我的快乐病毒、 2022-10-16 09:49 258阅读 0赞

Topic

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

Description

https://leetcode.com/problems/find-bottom-left-tree-value/

Given the root of a binary tree, return the leftmost value in the last row of the tree.

Example 1:

884cac6be3ebbce496bf9d4628361377.png

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

Example 2:

a71aa4cbc4614a6d485a0c9f24431e9f.png

  1. Input: root = [1,2,3,4,null,5,6,null,null,7]
  2. Output: 7

Constraints:

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

Analysis

方法一:BFS

方法二:BFS,比方法一的更简洁。与方法一的区别,本方法先添加右子树到队列,然后才是左子树

方法三:DFS

Submission

  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import com.lun.util.BinaryTree.TreeNode;
  4. public class FindBottomLeftTreeValue {
  5. //方法一:BFS
  6. public int findBottomLeftValue(TreeNode root) {
  7. LinkedList<TreeNode> queue = new LinkedList<>();
  8. queue.offer(root);
  9. TreeNode leftmost = null;
  10. while(!queue.isEmpty()) {
  11. int originalSize = queue.size();
  12. for(int size = originalSize; size > 0; size--) {
  13. TreeNode node = queue.poll();
  14. if(size == originalSize)
  15. leftmost = node;
  16. if(node.left != null)
  17. queue.offer(node.left);
  18. if(node.right != null)
  19. queue.offer(node.right);
  20. }
  21. }
  22. return leftmost.val;
  23. }
  24. //方法二:BFS,比方法一更简洁
  25. public int findBottomLeftValue2(TreeNode root) {
  26. Queue<TreeNode> queue = new LinkedList<>();
  27. queue.add(root);
  28. while (!queue.isEmpty()) {
  29. root = queue.poll();
  30. //从 右子树 到 左子数
  31. if (root.right != null)
  32. queue.add(root.right);
  33. if (root.left != null)
  34. queue.add(root.left);
  35. }
  36. return root.val;
  37. }
  38. //方法三:DFS
  39. public int findBottomLeftValue3(TreeNode root) {
  40. int[] leftmost = { 0}, depth = { 0};
  41. findBottomLeftValue3(root, 1, leftmost, depth);
  42. return leftmost[0];
  43. }
  44. private void findBottomLeftValue3(TreeNode node, int height, int[] leftmost, int[] depth) {
  45. if(node == null) return;
  46. if(height > depth[0]) {
  47. leftmost[0] = node.val;
  48. depth[0] = height;
  49. }
  50. findBottomLeftValue3(node.left, height + 1, leftmost, depth);
  51. findBottomLeftValue3(node.right, height + 1, leftmost, depth);
  52. }
  53. }

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 FindBottomLeftTreeValueTest {
  6. @Test
  7. public void test() {
  8. FindBottomLeftTreeValue obj = new FindBottomLeftTreeValue();
  9. TreeNode root1 = BinaryTree.integers2BinaryTree(2,1,3);
  10. TreeNode root2 = BinaryTree.integers2BinaryTree(1, 2, 3, 4, null, //
  11. 5, 6, null, null, 7);
  12. assertEquals(1, obj.findBottomLeftValue(root1));
  13. assertEquals(7, obj.findBottomLeftValue(root2));
  14. assertEquals(1, obj.findBottomLeftValue2(root1));
  15. assertEquals(7, obj.findBottomLeftValue2(root2));
  16. assertEquals(1, obj.findBottomLeftValue3(root1));
  17. assertEquals(7, obj.findBottomLeftValue3(root2));
  18. }
  19. }

发表评论

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

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

相关阅读