LeetCode872. Leaf-Similar Trees

2023-12-25 06:04
文章标签 trees leaf similar leetcode872

本文主要是介绍LeetCode872. 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.

Example 1:

Input: 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]
Output: true
Example 2:

Input: root1 = [1,2,3], root2 = [1,3,2]
Output: false

Constraints:

The number of nodes in each tree will be in the range [1, 200].
Both of the given trees will have values in the range [0, 200].

二、题解

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void preOrder(TreeNode* root,vector<int>& leaf){if(!root) return;if(root->left == nullptr && root->right == nullptr) leaf.push_back(root->val);preOrder(root->left,leaf);preOrder(root->right,leaf);}bool leafSimilar(TreeNode* root1, TreeNode* root2) {vector<int> leaf1;vector<int> leaf2;preOrder(root1,leaf1);preOrder(root2,leaf2);if(leaf1.size() != leaf2.size()) return false;for(int i = 0;i < leaf1.size();i++){if(leaf1[i] != leaf2[i]) return false;}return true;}
};

这篇关于LeetCode872. Leaf-Similar Trees的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/534458

相关文章

Unique Binary Search Trees II问题及解法

问题描述: Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. 示例: Given n = 3, your program should return all 5 unique BST's shown below. 1

Unique Binary Search Trees问题及解法

问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 示例: Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1\

Codeforces Round #236 (Div. 2) B. Trees in a Row

B. Trees in a Row time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The Queen of England has n trees growing in a row i

HDU 2841 Visible Trees 容斥

题意(一开始也没怎么看懂): 一个人站在(0,0)处,树从(1,1)点开始排,共有m*n棵。如果两棵树在同一视线上(意思是两个点和(0,0)的斜率相同),则只看到前面一棵树,问你那个人能看到几棵树。 思路: 容斥。 简单分析之后,其实本质就是让你求gcd(x,y)=1有几组。(x,y)和(y,x)算两种。 这题和HDU 1695差不多,只不过那题(x,y)和(y,x)算一种。

UVA 10303 How Many Trees?

题意:设f[i]表示一共有i个元素时二叉搜索树的个数,那么依次取1~n-1作为根节点,那么左右子树元素的个数就分别是(0,n-1),......,所以f[n] = f[0]*f[n-1]+f[1]*f[n-2]...+f[n-1]f[0],其实也就是Catalan数,高精度的计算,递推公式是f[n]=(4n-2)/(n+1)*f[n-1] #include <iostream>#include

HDU - 3015 Disharmony Trees

题意:给你n棵树的坐标x,高度h,让你分别按坐标,高度排序后,得到一新的序列,也可以理解为一颗组合成的新树,这棵树的坐标,高度都是排序来的,看了别人的解释,还是理解了老半天,然后就是求花费了,每任意两颗树的花费为 min(h[i],h[j])*abs(x[i]-x[j]),求所有组合的花费 思路:经过排序的处理后,就是仿着POJ-1990 的思想来做 了,也可以简化成:按高度排序后,然后按每棵

uva 10303 - How Many Trees?(卡特兰数)

题目链接:uva 10303 - How Many Trees? 卡特兰数,公式num[i + 1] = num[i] * (4 * i - 6) / i ( i ≥ 3)。 #include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int N = 6005;str

Codeforces Round 548 (Div. 2) C. Edgy Trees

Edgy Trees time limit per test: 2 second memory limit per test: 256 megabytes input: standard input output: standard output You are given a tree (a connected undirected graph without

RuntimeError: leaf variable has been moved into the graph interior(Pytorch报错)

有时候想在pytorch中修改训练过程中网络模型的参数。比如做网络稀疏化训练,对于某一层卷基层的参数,如果值小于一定阈值就想赋值为0,这时就需要实时修改网络模型的参数,如果直接修改会报错:         RuntimeError: leaf variable has been moved into the graph interior 这是因为pytorch中会有叶子张量和非叶子张量之分,这

KD-Trees(K-dimensional树)和Octrees(八叉树

KD-Trees(K-dimensional树)和Octrees(八叉树)是两种常用的数据结构,它们在多维空间中用于高效地存储和查询数据。这两种结构在计算机科学中有着广泛的应用,尤其是在图形学、机器人学、空间索引和最近邻搜索等领域。 KD-Trees KD-Trees是一种二叉树结构,用于组织K维空间中的点。在KD-Trees中,每个节点代表一个K维空间中的点,并且树是通过递归地将空间分割成两