LeetCode - Easy - 872. Leaf-Similar Trees

迈不过友情╰ 2022-10-06 06:54 124阅读 0赞

Topic

  • Tree
  • Depth-first Search

Description

https://leetcode.com/problems/leaf-similar-trees/

Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.

3b1bb158ebe9c3147e974f1fae08048e.png

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

Example 1:
987b3a71579326526e70439d1a28470c.png

  1. Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
  2. Output: true

Example 2:

  1. Input: root1 = [1], root2 = [1]
  2. Output: true

Example 3:

  1. Input: root1 = [1], root2 = [2]
  2. Output: false

Example 4:

  1. Input: root1 = [1,2], root2 = [2,2]
  2. Output: true

Example 5:

57d48edb37912116d948fad8f5a26be3.png

  1. Input: root1 = [1,2,3], root2 = [1,3,2]
  2. Output: false

Constraints:

  • The number of nodes in each tree will be in the range [1, 200].
  • Both of the given trees will have values in the range [0, 200].

Analysis

用到方便中断的迭代版前序遍历模式。

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class LeafSimilarTrees {
  4. public boolean leafSimilar(TreeNode root1, TreeNode root2) {
  5. LinkedList<TreeNode> stack1 = new LinkedList<>(), //
  6. stack2 = new LinkedList<>();
  7. stack1.push(root1);
  8. stack2.push(root2);
  9. while(true) {
  10. TreeNode leaf1 = getLeaf(stack1);
  11. TreeNode leaf2 = getLeaf(stack2);
  12. if(leaf1 == null ^ leaf2 == null) {
  13. return false;
  14. }else if(leaf1 == null && leaf2 == null) {
  15. return true;
  16. }else {
  17. if(leaf1.val != leaf2.val)
  18. return false;
  19. }
  20. }
  21. }
  22. private TreeNode getLeaf(LinkedList<TreeNode> stack) {
  23. while(!stack.isEmpty()) {
  24. TreeNode node = stack.pop();
  25. if(node.left == null && node.right == null)
  26. return node;
  27. if(node.right != null)
  28. stack.push(node.right);
  29. if(node.left != null)
  30. stack.push(node.left);
  31. }
  32. return null;
  33. }
  34. }

Test

  1. import static org.junit.Assert.*;
  2. import static com.lun.util.BinaryTree.integers2BinaryTree;
  3. import org.junit.Test;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class LeafSimilarTreesTest {
  6. @Test
  7. public void test() {
  8. LeafSimilarTrees obj = new LeafSimilarTrees();
  9. TreeNode root1 = integers2BinaryTree(3,5,1,6,2,9,8,null,null,7,4);
  10. TreeNode root2 = integers2BinaryTree(3,5,1,6,7,4,2,null,null,null,null,null,null,9,8);
  11. assertTrue(obj.leafSimilar(root1, root2));
  12. assertTrue(obj.leafSimilar(integers2BinaryTree(1), integers2BinaryTree(1)));
  13. assertFalse(obj.leafSimilar(integers2BinaryTree(1), integers2BinaryTree(2)));
  14. assertTrue(obj.leafSimilar(integers2BinaryTree(1, 2), integers2BinaryTree(2, 2)));
  15. assertFalse(obj.leafSimilar(integers2BinaryTree(1, 2, 3), integers2BinaryTree(1,3,2)));
  16. assertFalse(obj.leafSimilar(integers2BinaryTree(3,5,1,6,7,4,2,null,null,null,null,null,null,9,11,null,null,8,10),//
  17. integers2BinaryTree(3,5,1,6,2,9,8,null,null,7,4)));
  18. }
  19. }

发表评论

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

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

相关阅读