题目地址:

https://leetcode.com/problems/check-if-two-expression-trees-are-equivalent/

给定两个只含'+'和英文小写字母作为运算数的表达式树,问这两个表达式树所对应的表达式是否等价。等价的含义是两个表达式作为代数式用等号连起来是恒等式。

只需要数一下两个表达式树所含字母的类型、数量都相同即可。代码如下:

public class Solution {public boolean checkEquivalence(Node root1, Node root2) {int[] count = new int[26];dfs(root1, count, 1);dfs(root2, count, -1);for (int i = 0; i < count.length; i++) {if (count[i] != 0) {return false;}}return true;}private void dfs(Node cur, int[] count, int diff) {if (cur == null) {return;}if (cur.val != '+') {count[cur.val - 'a'] += diff;}dfs(cur.left, count, diff);dfs(cur.right, count, diff);}
}class Node {char val;Node left, right;public Node(char val) {this.val = val;}
}

时间复杂度 O ( n ) O(n) O(n),空间 O ( h ) O(h) O(h)。

【Leetcode】1612. Check If Two Expression Trees are Equivalent相关推荐

  1. 【Leetcode】1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree

    题目地址: https://leetcode.com/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-i ...

  2. 【LeetCode】2022 7月 每日一题

    [LeetCode]2022 7月 每日一题 前言 七月太忙了,又是项目又是练车又是各种比赛.大概有10天的每日一题没有当天写完(虽然后面补上了). 将每日一题的所有思路记录在这里分享一下. 7.1 ...

  3. 【leetcode】

    1. Two Sum [题目]https://leetcode.com/problems/two-sum/description/ [思路]将数组 利用 map 处理 即可 [代码] 1 class ...

  4. 【LeetCode】【HOT】114. 二叉树展开为链表(原地置换)

    [LeetCode][HOT]114. 二叉树展开为链表 文章目录 [LeetCode][HOT]114. 二叉树展开为链表 package hot;import java.util.ArrayLis ...

  5. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  6. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  7. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  8. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  9. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

最新文章

  1. 《LeetCode力扣练习》第10题 C语言版 (做出来就行,别问我效率。。。。)
  2. 基于fdatool的滤波器设计(低通、带通、高通)
  3. win7-64bit 下oracle11g plsql 的正确安装
  4. 02.CCNA 200-301 题库_51-100
  5. 89C51单片机内部结构深度解析
  6. Cox模型中的时间依存协变量和时间依存系数(R语言)第二部分
  7. 打开.sql文件的方法
  8. 杨澜对话刘韵洁董明珠 :“穷得只有一个梦想”
  9. DSPE-PEG-TH(AGYLLGHINLHHLAHL(Aib)HHIL) 磷脂(二硬脂酰基磷脂酰乙醇胺)-聚乙二醇-TH(AGYLLGHINLHHLAHL(Aib)HHIL)
  10. 基于机智云的智能花盆2.0
  11. webpack合成sprite图
  12. python 阿里云平台合成语音(TTS)
  13. 防晒新时代,小红书美妆品牌营销趋势洞察
  14. 中国大学MOOC音乐与健康试题及答案
  15. ccsp2018游记
  16. Qt通讯录(语音输入+发邮件+发短信+头像)
  17. Footprint Expert PRO 22 - 修改封装的管脚编号
  18. 几个有趣的经济学故事
  19. 提高科技创新能力和国际竞争力,加强和改进科学技术管理体制,促进
  20. 图像处理方面的sci期刊_图像处理 SCI期刊有哪些?

热门文章

  1. 28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(解读误区)
  2. 论文笔记:Neural Collaborative Filtering(NCF)
  3. [Tushare+Python] AH股轮动策略基础
  4. 也谈男士品牌(综合版)
  5. GlusterFS基本部署
  6. 修改图片颜色 色相 饱和度 亮度 ColorMatrix
  7. VMware vRealize Automation 8 Install
  8. 如何用计算机模拟基金收益,[原创]中国股市十七年的基金计算机模拟
  9. 网上商品交易网站,怎样做到信息安全
  10. 华为认证——HCIA-IoT(V2.0)——物联网工程师-模拟试卷(答案)