题目

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.

讲解

这道题的思路很简单,先扫描出两个树的叶子结果集,然后比较就行了。考察点是树的遍历。递归的时候首先要判断结点是否为空。

Java代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public boolean leafSimilar(TreeNode root1, TreeNode root2) {List<Integer> leaves1 = new ArrayList<>();List<Integer> leaves2 = new ArrayList<>();getLeaves(root1, leaves1);getLeaves(root2, leaves2);if(leaves1.size()!=leaves2.size()){return false;}else{for(int i=0;i<leaves1.size();i++){if(leaves1.get(i)!=leaves2.get(i)){return false;}}}return true;}private void getLeaves(TreeNode root, List<Integer> leaves){if(root==null){return;}if(root.left==null && root.right==null){leaves.add(root.val);return;}getLeaves(root.left, leaves);getLeaves(root.right, leaves);}
}

leetcode讲解--872. Leaf-Similar Trees相关推荐

  1. 【重点 递归构造二叉树】LeetCode 95. Unique Binary Search Trees II

    LeetCode 95. Unique Binary Search Trees II 本博客转载自:[1]https://segmentfault.com/a/1190000007443961 [2] ...

  2. 【卡塔兰数】LeetCode 96. Unique Binary Search Trees

    LeetCode 96. Unique Binary Search Trees 本博客转载自:http://www.cnblogs.com/grandyang/p/4299608.html Solut ...

  3. LeetCode讲解视频博主链接

    LeetCode讲解视频博主链接 最近在YouTube上发现了一个讲解LeetCode题目的博主 花花酱,可以参考! 博客地址:http://zxi.mytechroad.com/blog/ 视频链接 ...

  4. Leetcode 742. Closest Leaf in a Binary Tree

    Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...

  5. leetcode讲解--559. Maximum Depth of N-ary Tree

    题目 Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the lo ...

  6. leetcode 95. Unique Binary Search Trees II | 96. Unique Binary Search Trees

    95. Unique Binary Search Trees II https://leetcode.com/problems/unique-binary-search-trees-ii/ 题解 题是 ...

  7. [LeetCode]: 96: Unique Binary Search Trees

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

  8. leetcode讲解--566. Reshape the Matrix

    题目 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...

  9. LeetCode 1548. The Most Similar Path in a Graph(动态规划)

    文章目录 1. 题目 2. 解题 1. 题目 We have n cities and m bi-directional roads where roads[i] = [ai, bi] connect ...

最新文章

  1. elasticsearch实现按天翻滚索引
  2. 【CF888G】Xor-MST(最小生成树,Trie树)
  3. pythontk多线程_tkinter是否线程安全(threadsafe)?
  4. 2008高考零分作文---大事与小事
  5. Flume实战监听文件夹内文件变化
  6. php数组连起来,PHP 数组的拼接重组
  7. imagej边缘提取
  8. 别以为太简单!创建最好邮件营销内容的13个技巧
  9. 关于云流化系统-实时云渲染延时性的讨论
  10. Python 常用迭代函数总结
  11. 通过泰勒展开求自然常数e,R语言实现
  12. 大学生python实验心得体会_大学生实训心得体会3篇
  13. Boost(一)——Boost简介
  14. 如何看待快码编程这一款中文多平台编程工具
  15. 怎样查询本机ip地址?如何利用花生壳获取外网IP教程
  16. 【算法】最长公共子序列(LCS)
  17. DDR3 MIG上板测试记录
  18. 【燃料电池】基于simulink的燃料电池系统控制策略仿真
  19. java xfire 客户端代码_java调用xfire webService服务客户端代码
  20. 总结证书扩展和证书分类

热门文章

  1. 通过java提供的URL类包读取网上的文件
  2. MySQL Performance-Schema(三) 实践篇
  3. GC垃圾回收的三色标记算法
  4. 解决wiremock中velocity脚本(.vm)中文编码乱码问题
  5. SDNU 1178.能量项链(区间dp)
  6. ubuntu18.04(bionic) 配置阿里数据源
  7. RHEL 6.5 + Oracle 11g安装
  8. Spring依赖注入:注解注入总结
  9. [译]管理IIS日志的存储
  10. 【Python②】python之首秀