872:叶子相似的树

This commit is contained in:
huangge1199 2021-05-10 14:17:43 +08:00
parent 758e06416a
commit 970a919cac
3 changed files with 187 additions and 1 deletions

View File

@ -0,0 +1,126 @@
//请考虑一棵二叉树上所有的叶子这些叶子的值按从左到右的顺序排列形成一个 叶值序列
//
//
//
// 举个例子如上图所示给定一棵叶值序列为 (6, 7, 4, 9, 8) 的树
//
// 如果有两棵二叉树的叶值序列是相同那么我们就认为它们是 叶相似
//
// 如果给定的两个根结点分别为 root1 root2 的树是叶相似的则返回 true否则返回 false
//
//
//
// 示例 1
//
//
//
//
//输入root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,nul
//l,null,null,null,9,8]
//输出true
//
//
// 示例 2
//
//
//输入root1 = [1], root2 = [1]
//输出true
//
//
// 示例 3
//
//
//输入root1 = [1], root2 = [2]
//输出false
//
//
// 示例 4
//
//
//输入root1 = [1,2], root2 = [2,2]
//输出true
//
//
// 示例 5
//
//
//
//
//输入root1 = [1,2,3], root2 = [1,3,2]
//输出false
//
//
//
//
// 提示
//
//
// 给定的两棵树可能会有 1 200 个结点
// 给定的两棵树上的值介于 0 200 之间
//
// Related Topics 深度优先搜索
// 👍 129 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
//872:叶子相似的树
public class LeafSimilarTrees {
public static void main(String[] args) {
//测试代码
Solution solution = new LeafSimilarTrees().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> list1 = getLeaf(root1, new ArrayList<>());
List<Integer> list2 = getLeaf(root2, new ArrayList<>());
if (list1.size() != list2.size()) {
return false;
}
for (int i = 0; i < list1.size(); i++) {
if (!list1.get(i).equals(list2.get(i))) {
return false;
}
}
return true;
}
private List<Integer> getLeaf(TreeNode root, List<Integer> list) {
if (root.left == null && root.right == null) {
list.add(root.val);
return list;
}
if (root.left != null) {
list = getLeaf(root.left, list);
}
if (root.right != null) {
list = getLeaf(root.right, list);
}
return list;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,60 @@
<p>请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 <em>叶值序列</em></p>
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png" style="height: 240px; width: 300px;" /></p>
<p>举个例子,如上图所示,给定一棵叶值序列为 <code>(6, 7, 4, 9, 8)</code> 的树。</p>
<p>如果有两棵二叉树的叶值序列是相同,那么我们就认为它们是 <em>叶相似 </em>的。</p>
<p>如果给定的两个根结点分别为 <code>root1</code> 和 <code>root2</code> 的树是叶相似的,则返回 <code>true</code>;否则返回 <code>false</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg" style="height: 297px; width: 750px;" /></p>
<pre>
<strong>输入:</strong>root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
<strong>输出:</strong>true
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>root1 = [1], root2 = [1]
<strong>输出:</strong>true
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>root1 = [1], root2 = [2]
<strong>输出:</strong>false
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>root1 = [1,2], root2 = [2,2]
<strong>输出:</strong>true
</pre>
<p><strong>示例 5</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg" style="height: 165px; width: 450px;" /></p>
<pre>
<strong>输入:</strong>root1 = [1,2,3], root2 = [1,3,2]
<strong>输出:</strong>false
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>给定的两棵树可能会有 <code>1</code> 到 <code>200</code> 个结点。</li>
<li>给定的两棵树上的值介于 <code>0</code><code>200</code> 之间。</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>深度优先搜索</li></div></div>\n<div><li>👍 129</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long