LeetCode: 872. 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.

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.

Note:

Both of the given trees will have between 1 and 100 nodes.

题目大意:求给定两个树的叶子节点从左到右构成的序列是否相等。

解题思路

分别中序遍历两棵树,按顺序记录各自的叶子,然后比较是否相等。

AC 代码

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {void getLeaves(TreeNode* root, list<int>& leaves){if(root == nullptr) return ;if(root->left == nullptr && root->right == nullptr){leaves.push_back(root->val);return;}getLeaves(root->left, leaves);getLeaves(root->right, leaves);}
public:bool leafSimilar(TreeNode* root1, TreeNode* root2) {list<int> leaves1, leaves2;getLeaves(root1, leaves1);getLeaves(root2, leaves2);return (leaves1 == leaves2);}
};

LeetCode: 872. Leaf-Similar Trees相关推荐

  1. LeetCode | 872. Leaf-Similar Trees

    . 题目 Consider all the leaves of a binary tree, from left to right order, the values of those leaves ...

  2. leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)

    题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DF ...

  3. LeetCode Unique Binary Search Trees(dp)

    问题:给出一个正整数n,求由1到n所组成的二叉搜索树的个数 思路: 动态规划法.用f(i)表示由1到i所组成的二叉搜索树的个数.状态转移方程为 初始状态为 具体代码参考: https://github ...

  4. LeetCode Unique Binary Search Trees II(递归或者dp)

    问题:给出一个正整数,要求构造由1到n组成的所有的二叉搜索树 思路: 1.递归法 在求从1到n的二叉搜索树时,枚举中间值i(1到n),求[1,i-1]和[i+1,n]分别构成的二叉搜索树,然后根据左右 ...

  5. LeetCode: Unique Binary Search Trees [095]

    [题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...

  6. LeetCode:Unique Binary Search Trees

    问题描写叙述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? F ...

  7. [leetcode] 872. 叶子相似的树(周赛)

    872. 叶子相似的树 前序遍历,记录叶子节点即可 class Solution {private static String ans = "";public boolean le ...

  8. LeetCode 872叶子相似的树-简单

    请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7, 4, 9, 8) 的树. 如果有两棵二叉树的叶值序列是相同 ...

  9. leetcode 872. 叶子相似的树(dfs)

    请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7, 4, 9, 8) 的树. 如果有两棵二叉树的叶值序列是相同 ...

  10. LeetCode 310. Minimum Height Trees

    文章目录 知识点 结果 菜鸡的DFS+记忆化 网友的BFS"剥洋葱" 实现 菜鸡的DFS+记忆化 代码 反思 网友的BFS"剥洋葱" 代码 反思 知识点 图的遍 ...

最新文章

  1. 深入理解JVM(二)--垃圾收集算法
  2. 4道经典指针笔试题讲解 ~
  3. 【计算理论】计算复杂性 ( 多项式等价 | P 类 | 丘奇-图灵论题延伸 )
  4. HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
  5. java android统计图_Android统计图表之柱状图(条形图)
  6. libcoredb.class.php,ThinkPHP/Lib/Core/Db.class.php中pdo处理逻辑似乎不完善,导致config中pdo配置失效...
  7. IDEA生成可运行jar包
  8. [导入]存储过程-分隔符号-多条件查询
  9. 模版 ----- DP
  10. 用sql写每年的第三周_如何写好5000行的SQL代码
  11. 最新稳定短视频去水印免费解析API接口分享
  12. Android 7.0以上安装fiddler CA证书失败解决方法
  13. 局域网常见攻击方式原理
  14. 【华为 OJ 】等差数列
  15. java基础(三):java面向对象OOP
  16. mysql 汉字转成拼音
  17. windows客户端开发--也许是一条不归路
  18. pwm控制电机转速原理浅析
  19. FCPX自定义分屏字幕插件:Stupid Raisins Split Pop效果展示
  20. 用Mpvue开发微信小程序,微信语音聊天

热门文章

  1. plink遗传数据质控--每个个体QC、每个marker(变异)质控、全基因组关联meta分析QC
  2. Mybatis|CURD|配置详解|注解开发|多点查询|动态sql|缓存
  3. 解决RabbitMQ无法使用guest用户登录问题
  4. [VS2010]逸雨清风 永久稳定音乐外链生成软件V0.1
  5. 经典再现,看到就是赚到。尚硅谷雷神 - SpringBoot 2.x 学习笔记 - 核心功能篇
  6. ICCV 2021 | 当Transformer遇见自监督学习!Facebook重磅开源DINO
  7. 码住!Elsevier-最全清样/校样(Proof)处理流程
  8. Linux容器的安装链接
  9. thrift编写规则,及常见问题
  10. Java8 Stream,过分丝滑!