Equivalent sorted binary tree In Java

huangapple go评论113阅读模式
英文:

Equivalent sorted binary tree In Java

问题

我正在学习Go,在这个教程中,可以使用并发和通道来完成这个练习:解决方案

我尝试用Java来解决这个问题。我能想到的解决办法是使用临时数据结构来存储这两棵树的中序遍历结果,然后进行比较。

例如,我使用一个StringBuilder来存储中序遍历的结果,然后进行比较(注意我们正在比较排序后的二叉树):

  1. public boolean equivalentBST(TreeNode p, TreeNode q) {
  2. StringBuilder pSb = new StringBuilder();
  3. walk(pSb, p);
  4. StringBuilder qSb = new StringBuilder();
  5. walk(qSb, q);
  6. return pSb.compareTo(qSb) == 0;
  7. }
  8. private void walk(StringBuilder sb, TreeNode node) {
  9. if (node == null) {
  10. return;
  11. }
  12. walk(sb, node.left);
  13. sb.append(node.val);
  14. walk(sb, node.right);
  15. }

测试用例:

  1. TreeNode p = new TreeNode(9);
  2. TreeNode p2 = new TreeNode(8);
  3. TreeNode p3 = new TreeNode(7);
  4. p.left = p2;
  5. p2.left = p3;
  6. TreeNode q = new TreeNode(9);
  7. TreeNode q2 = new TreeNode(8);
  8. TreeNode q3 = new TreeNode(7);
  9. q3.right = q2;
  10. q.left = q3;
  11. System.out.println(equivalentBST(q, p)); // 输出 true

我想知道是否有其他更好的方法来用Java解决这个问题?

英文:

I am learning Go and in this tutorial, concurrency and channels can be used to complete this exercise: Solution.

And I try to solve this by Java. The solution I can think of is to use temporary data structure to store the results of the in-order traversal of these two trees, and then compare.

For example, I use a StringBuilder to store the result of the in-order traversal and then compare(Notice that we're comparing sorted binary trees):

  1. public boolean equivalentBST(TreeNode p, TreeNode q) {
  2. StringBuilder pSb = new StringBuilder();
  3. walk(pSb, p);
  4. StringBuilder qSb = new StringBuilder();
  5. walk(qSb, q);
  6. return pSb.compareTo(qSb) == 0;
  7. }
  8. private void walk(StringBuilder sb, TreeNode node) {
  9. if (node == null) {
  10. return;
  11. }
  12. walk(sb, node.left);
  13. sb.append(node.val);
  14. walk(sb, node.right);
  15. }

Testcase:

  1. TreeNode p = new TreeNode(9);
  2. TreeNode p2 = new TreeNode(8);
  3. TreeNode p3 = new TreeNode(7);
  4. p.left = p2;
  5. p2.left = p3;
  6. TreeNode q = new TreeNode(9);
  7. TreeNode q2 = new TreeNode(8);
  8. TreeNode q3 = new TreeNode(7);
  9. q3.right = q2;
  10. q.left = q3;
  11. System.out.println(equivalentBST(q, p)); // output true

I want to know is there any other better way to solve this by Java?

答案1

得分: 0

你可以同时遍历这两棵树,并立即比较它们的元素,而无需使用 StringBuilder

大致如下所示:

  1. public boolean equivalentBST(TreeNode node1, TreeNode node2) {
  2. var stack1 = new ArrayList<TreeNode>();
  3. var stack2 = new ArrayList<TreeNode>();
  4. while (true) {
  5. while (node1 != null) {
  6. stack1.add(node1);
  7. node1 = node1.left;
  8. }
  9. while (node2 != null) {
  10. stack2.add(node2);
  11. node2 = node2.left;
  12. }
  13. if (stack1.isEmpty() || stack2.isEmpty())
  14. return stack1.isEmpty() && stack2.isEmpty();
  15. node1 = stack1.remove(stack1.size() - 1);
  16. node2 = stack2.remove(stack2.size() - 1);
  17. if (node1.val != node2.val)
  18. return false;
  19. node1 = node1.right;
  20. node2 = node2.right;
  21. }
  22. }

希望对你有帮助!

英文:

You can walk both trees at the same time and compare their elements immediately without using a StringBuilder.

Something along the lines:

  1. public boolean equivalentBST(TreeNode node1, TreeNode node2) {
  2. var stack1 = new ArrayList&lt;TreeNode&gt;();
  3. var stack2 = new ArrayList&lt;TreeNode&gt;();
  4. while (true) {
  5. while (node1 != null) {
  6. stack1.add(node1);
  7. node1 = node1.left;
  8. }
  9. while (node2 != null) {
  10. stack2.add(node2);
  11. node2 = node2.left;
  12. }
  13. if (stack1.isEmpty() || stack2.isEmpty())
  14. return stack1.isEmpty() &amp;&amp; stack2.isEmpty();
  15. node1 = stack1.remove(stack1.size() - 1);
  16. node2 = stack2.remove(stack2.size() - 1);
  17. if (node1.val != node2.val)
  18. return false;
  19. node1 = node1.right;
  20. node2 = node2.right;
  21. }
  22. }

huangapple
  • 本文由 发表于 2021年12月29日 19:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70518817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定