189.Leaf-Similar Trees

2024-05-12 00:38
文章标签 trees 189 leaf similar

本文主要是介绍189.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 and 100 nodes.

链接

https://leetcode.com/problems/leaf-similar-trees/

分析

题目意思转化为找一个二叉树的自左向右的叶子节点的顺序;

code

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution(object):def leafSimilar(self, root1, root2):""":type root1: TreeNode:type root2: TreeNode:rtype: bool""""""题目意思转化为找一个二叉树的自左向右的叶子节点的顺序;采用递归的思想做;(1). root 的左右节点为空,则为叶子节点加入到最后的 list 中;(2). 否则,遍历左右子树"""return self.getLeafVal(root1) == self.getLeafVal(root2)def getLeafVal(self, root):""":type root: TreeNode"""if root == None:return []re = [] ## 存放最后的结果if root.left == None and root.right == None:re.append(root.val)else:re += self.getLeafVal(root.left)re += self.getLeafVal(root.right)return re

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



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

相关文章

189.二叉树:将有序数组转换为二叉搜索树(力扣)

代码解决 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(in

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中会有叶子张量和非叶子张量之分,这