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
and100
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相关推荐
- 【重点 递归构造二叉树】LeetCode 95. Unique Binary Search Trees II
LeetCode 95. Unique Binary Search Trees II 本博客转载自:[1]https://segmentfault.com/a/1190000007443961 [2] ...
- 【卡塔兰数】LeetCode 96. Unique Binary Search Trees
LeetCode 96. Unique Binary Search Trees 本博客转载自:http://www.cnblogs.com/grandyang/p/4299608.html Solut ...
- LeetCode讲解视频博主链接
LeetCode讲解视频博主链接 最近在YouTube上发现了一个讲解LeetCode题目的博主 花花酱,可以参考! 博客地址:http://zxi.mytechroad.com/blog/ 视频链接 ...
- Leetcode 742. Closest Leaf in a Binary Tree
Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...
- 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 ...
- 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/ 题解 题是 ...
- [LeetCode]: 96: Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- leetcode讲解--566. Reshape the Matrix
题目 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- 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 ...
最新文章
- elasticsearch实现按天翻滚索引
- 【CF888G】Xor-MST(最小生成树,Trie树)
- pythontk多线程_tkinter是否线程安全(threadsafe)?
- 2008高考零分作文---大事与小事
- Flume实战监听文件夹内文件变化
- php数组连起来,PHP 数组的拼接重组
- imagej边缘提取
- 别以为太简单!创建最好邮件营销内容的13个技巧
- 关于云流化系统-实时云渲染延时性的讨论
- Python 常用迭代函数总结
- 通过泰勒展开求自然常数e,R语言实现
- 大学生python实验心得体会_大学生实训心得体会3篇
- Boost(一)——Boost简介
- 如何看待快码编程这一款中文多平台编程工具
- 怎样查询本机ip地址?如何利用花生壳获取外网IP教程
- 【算法】最长公共子序列(LCS)
- DDR3 MIG上板测试记录
- 【燃料电池】基于simulink的燃料电池系统控制策略仿真
- java xfire 客户端代码_java调用xfire webService服务客户端代码
- 总结证书扩展和证书分类