872. Leaf-Similar Trees - LeetCode
Question
872. Leaf-Similar Trees
Solution
题目大意:
如果两个二叉树的叶子节点相同就认为这两个二叉树相似。给两个二叉树判断是否相似。
思路:
用递归把两个二叉树的叶子节点遍历出来,再比较叶子节点是否相同
Java实现:
public boolean leafSimilar(TreeNode root1, TreeNode root2) {// 把叶子节点遍历出来,比较List<Integer> leftLeafs = new ArrayList<>();List<Integer> rightLeafs = new ArrayList<>();initLeaf(root1, leftLeafs);initLeaf(root2, rightLeafs);if (leftLeafs.size() != rightLeafs.size()) return false;for (int i = 0; i < leftLeafs.size(); i++) {if (leftLeafs.get(i) != rightLeafs.get(i)) return false;}return true;
}public void initLeaf(TreeNode root, List<Integer> leafs) {if (root.left == null && root.right == null) leafs.add(root.val);if (root.left != null) initLeaf(root.left, leafs);if (root.right != null) initLeaf(root.right, leafs);
}
转载于:https://www.cnblogs.com/okokabcd/p/9592822.html
872. Leaf-Similar Trees - LeetCode相关推荐
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode: 111. Minimum Depth of Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
- 寒假LeetCode打卡
文章目录 @[toc] 链表专题 LeetCode 19. Remove Nth Node From End of List LeetCode 83. Remove Duplicates from S ...
- STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树
STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树 最近在做项目时发现一个crash的问题,当时得到的dmp文件显示crash在一个以map为循环变量的循环中,crash位置在如下 ...
- 中信银行面试前准备好多材料_面试准备帮助我在锁定期间获得了多个报价
中信银行面试前准备好多材料 Preparing for interviews can be overwhelming, especially when you have a full-time job ...
- 机器学习论文:《LightGBM: A Highly Efficient Gradient Boosting Decision Tree》
翻译自<LightGBM: A Highly Efficient Gradient Boosting Decision Tree> 摘要 Gradient Boosting Decisio ...
- leetcode讲解--872. Leaf-Similar Trees
题目 Consider all the leaves of a binary tree. From left to right order, the values of those leaves fo ...
- LeetCode | 872. Leaf-Similar Trees
. 题目 Consider all the leaves of a binary tree, from left to right order, the values of those leaves ...
- leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)
题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DF ...
最新文章
- 贴别人的一个文件加密程序!
- Centos设置开机启动Apache和Mysql[总结]
- java无法获取套接字_无法从套接字获取更多数据
- python使用符号#表示单行注释-Pyhton中单行和多行注释的使用方法及规范
- 支持XML的公司和它们的开发工具有哪些?
- 1074. 元素和为目标值的子矩阵数量
- 最全面的MySQL面试题大全-二
- PyCharm 下提示 'no module named time'
- linux导航改端口波特率,各大导航端口速率修改方法
- 解决Mac无法睡眠问题
- 数论之费马大定理及怀尔斯的证明
- C# 将系统时间转换成农历时间
- 网站快照被劫持解决办法:织梦程序
- python实现word自动化操作
- springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式
- 2022-03-27 screenX和clientX的区别以及offsetX和pageX的区别
- VS编译错误:mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file
- 嘿!不备案不要HTTPS!小程序请求任意网站黑科技来了!
- mac上的Finder快捷键汇总
- 动态排程才是生产计划排程的根本
热门文章
- GDB+coredump定位段错误
- python xml解析cdata_python基于xml parse实现解析cdatasection数据
- 计算机培训中学语文研修计划,初中语文个人研修计划书
- Redis 6.0 源码阅读笔记(5) -- List 数据类型源码分析
- 设计模式---命令模式(C++实现)
- MAC VSCode Go代码第一次运行配置
- 6.5移相器(Phasers)
- JSP 的“4379”
- [渝粤教育] 江苏食品药品职业技术学院 食品生物化学 参考 资料
- 【渝粤教育】电大中专计算机职业素养 (7)作业 题库