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

相关文章

Closest Leaf in a Binary Tree

Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree:1/ \2 3/4/5/6Output: 3Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the no

189.轮转数组

189.轮转数组 给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 示例 1: 输入: nums = [1,2,3,4,5,6,7], k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右轮转 1 步: [7,1,2,3,4,5,6] 向右轮转 2 步: [6,7,1,2,3,4,5] 向右轮转 3 步: [5,6,7,1,2,3,4] 示例

【LeetCode】Unique Binary Search Trees I II

1、Unique Binary Search Trees  Total Accepted: 11405 Total Submissions: 32197 My Submissions Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example

leetcode-Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 去网上搜n个二叉搜索树的递推公式或者Catalan数,可以由h(n)=C(

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