LeetCode - Easy - 100. Same Tree

朴灿烈づ我的快乐病毒、 2022-12-28 00:59 222阅读 0赞

Topic

Tree, Depth-first Search

Description

https://leetcode.com/problems/same-tree/

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

  1. Input: 1 1
  2. / \ / \
  3. 2 3 2 3
  4. [1,2,3], [1,2,3]
  5. Output: true

Example 2:

  1. Input: 1 1
  2. / \
  3. 2 2
  4. [1,2], [1,null,2]
  5. Output: false

Example 3:

  1. Input: 1 1
  2. / \ / \
  3. 2 1 1 2
  4. [1,2,1], [1,1,2]
  5. Output: false

Analysis

方法一:递归前序遍历

方法二:非递归前序遍历

Submission

  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import com.lun.util.BinaryTree.TreeNode;
  4. public class SameTree {
  5. //方法一:递归前序遍历
  6. public boolean isSameTree1(TreeNode p, TreeNode q) {
  7. if (p == null && q != null || p != null && q == null)
  8. return false;
  9. if (p != null && q != null) {
  10. if (p.val != q.val) {
  11. return false;
  12. }
  13. // 判断左右子树
  14. return isSameTree1(p.left, q.left) && isSameTree1(p.right, q.right);
  15. }
  16. return true;
  17. }
  18. //方法二:非递归前序遍历
  19. public boolean isSameTree2(TreeNode root1, TreeNode root2) {
  20. TreeNode p = root1, q = root2;
  21. LinkedList<TreeNode> stack = new LinkedList<>();
  22. while(true) {
  23. if(p == null && q != null || p != null && q == null) {
  24. return false;
  25. }
  26. if(p != null && q != null) {
  27. if(p.val != q.val) {
  28. return false;
  29. }
  30. //下列push的精简
  31. stack.addAll(0, Arrays.asList(p.left, q.left, p.right, q.right));
  32. // stack.push(q.right);
  33. // stack.push(p.right);
  34. //
  35. // stack.push(q.left);
  36. // stack.push(p.left);
  37. }
  38. if(stack.isEmpty()) {
  39. break;
  40. }else {
  41. p = stack.pop();
  42. q = stack.pop();
  43. }
  44. }
  45. return true;
  46. }
  47. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree.TreeNode;
  4. public class SameTreeTest {
  5. @Test
  6. public void test() {
  7. SameTree obj = new SameTree();
  8. TreeNode root1 = new TreeNode(1);
  9. root1.left = new TreeNode(2);
  10. root1.right = new TreeNode(3);
  11. TreeNode root2 = new TreeNode(1);
  12. root2.left = new TreeNode(2);
  13. root2.right = new TreeNode(3);
  14. assertTrue(obj.isSameTree1(root1, root2));
  15. assertTrue(obj.isSameTree2(root1, root2));
  16. }
  17. @Test
  18. public void test2() {
  19. SameTree obj = new SameTree();
  20. TreeNode root1 = new TreeNode(1);
  21. root1.left = new TreeNode(2);
  22. TreeNode root2 = new TreeNode(1);
  23. root2.right = new TreeNode(2);
  24. assertFalse(obj.isSameTree1(root1, root2));
  25. assertFalse(obj.isSameTree2(root1, root2));
  26. }
  27. @Test
  28. public void test3() {
  29. SameTree obj = new SameTree();
  30. TreeNode root1 = new TreeNode(1);
  31. root1.left = new TreeNode(2);
  32. root1.right = new TreeNode(1);
  33. TreeNode root2 = new TreeNode(1);
  34. root2.right = new TreeNode(1);
  35. root2.right = new TreeNode(2);
  36. assertFalse(obj.isSameTree1(root1, root2));
  37. assertFalse(obj.isSameTree2(root1, root2));
  38. }
  39. }

发表评论

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

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

相关阅读

    相关 #100 Same Tree

    按照通过率,第三的需要买书(还是过段时间再说吧……),那么就来做第四题咯 [\100 Same Tree][100 Same Tree] 这道题同样很简单,一看就是需