Q4.8 To decide if T2 is a subtree of T1

【事件相机实战】噪声识别与智能滤波技术解析 本文深入解析了事件相机应用中关键的噪声识别与智能滤波技术。针对背景活动噪声、阈值波动噪声、热点噪声及运动边缘尾迹等核心噪声类型,文章剖析了其成因与特征,并详细介绍了从经典的时空相关性滤波、热点像素滤除到前沿的深度学习(如EDnCNN)等实战滤波方法,旨在帮助开发者在保证低延迟的前提下,有效提升事件流数据质量。 阅读详情

Q:You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1

A:DFS.

T1当前节点是否与T2的根节点相等。

如果不相等,查看T2是不是T1的左、右子树的子树

如果相等,查看T1的左右子树和T2的左右子树是否一致


#include <iostream>
#include <vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() {}
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//初始化数据 
TreeNode *dfs(vector<int> &num, int start, int end) {
    if (start == end) {
        return NULL;
    }
    int mid = start + (end-start)/2;
    TreeNode *root = new TreeNode(num[mid]);
    root->left = dfs(num, start, mid);
    root->right = dfs(num, mid+1, end);
    return root;
}

TreeNode *sortedArrayToBST(vector<int> &num) {
    const int n = num.size();
    if (n == 0) {
        return NULL;
    }
    return dfs(num,0,n);
}
    
//算法实现 
bool matchTree(TreeNode *p, TreeNode *q) {
	if (!p && !q) {
		return true;
	}
	if (!p || !q) {
		return false;
	}
	if (p->val != q->val) {
		return false;
	} 
	return matchTree(p->left, q->left) && matchTree(p->right, q->right);
	
}

bool subTree(TreeNode *root1, TreeNode *root2) {
	if (!root1) {
		return false;
	}
	if (root1->val == root2->val) {
		return matchTree(root1, root2);
	}
	return matchTree(root1->left, root2)||matchTree(root1->right, root2);
}

bool containsTree(TreeNode *root1, TreeNode *root2) {
	if (!root2) {
		return true;
	}
	return subTree(root1, root2);
}


vector<int> inorderTraversal(TreeNode *root) {  
    vector<int> res;  
    if (!root) {  
        return res;  
    }  
    TreeNode *cur = root;  
    while (cur) {  
        if (cur->left == NULL) {  
            res.push_back(cur->val);  
            cur = cur->right;  
        } else {  
            TreeNode *node = cur->left;  
            while (node->right && node->right != cur) {  
                node = node->right;  
            }  
            if (!node->right) {  
                node->right = cur;  
                cur = cur->left;  
            } else {  
                res.push_back(cur->val);  
                node->right = NULL;  
                cur = cur->right;  
            }  
        }  
    }  
      
      
    return res;  

    }  

int main() {
	vector<int> a;
	for (int i = 1; i < 10; i++) {
		a.push_back(i);
	}
	TreeNode *root1 = sortedArrayToBST(a);
	a.clear();
	a = inorderTraversal(root1);
	for (int i = 0; i < a.size(); i++) {
		cout<<a[i]<<" ";
	}
	cout<<endl;
	vector<int> b;
	for (int i = 1; i < 5; i++) {
		b.push_back(i);
	}
	TreeNode *root2 = sortedArrayToBST(b);
	b.clear();
	b = inorderTraversal(root2);
	for (int i = 0; i < b.size(); i++) {
		cout<<b[i]<<" ";
	}
	cout<<endl;
	cout<<containsTree(root1, root2)<<endl;
	return 0;
}


Rijndael密码算法 一 什么是Rijndael Rijndael是由比利时密码学家设计的分组密码算法,于2000年被选为新一代的标准密码算法——AES。今后会有越来越多的密码软件支持这种算法。 Rijndael的分组长度和密钥长度可以分别以32比特为单位在128比特和256比特的范围内进行选择。不过在AES的规格中,分组长度固定为128比特,密钥长度只有128、192和256比特三种。 二 Rijndael的加... 阅读详情

相关推荐

IC版图设计之OASIS文件格式详解

OASIS(Open Artwork System Interchange Standard)是继GDSII之后新一代的集成电路版图数据交换标准,由SEMI在2004年正式发布(SEMI标准号:SEMI P39)。高压缩率:相比GDSII可减少3-10倍文件体积扩展性强:支持纳米级工艺的复杂图形描述跨平台兼容:独立于EDA工具和操作系统安全增强:内置校验和与加密支持。

ZZcppc的博客 2440

Isomorphic

Two trees, T1 and T2, are isomorphic if T1 can be transformed into T2 by swapping left and right children of (some of the) nodes in T1. For instance, the two trees in Figure 1 are isomorphic because

dxz935的博客 3526

Metal 一、初识 Metal 及其语言规范

游戏开发者的新的技术平台,该技术能够为3D图像提高 10 倍的渲染性能。苹果2018年推出 Metal,在此之前一直使用OpenGL ES。1图形渲染;2高并发计算。苹果文档给出的 Metal 优化:CPU 低消耗、GPU 高利用、连续处理器并行、有效的资源利用Metal 官方文档。

irainsa的博客 1219

6-7 Isomorphic(20 分)

6-7 Isomorphic(20 分)Two trees, T1 and T2, are isomorphic if T1 can be transformed into T2 by swapping left and right children of (some of the) nodes in T1. For instance, the two trees in Figure 1 are ...

友人A 3490

虚幻UE 增强输入-触发器

输入触发器(UInputTrigger): 使用经过修饰器修改的输入值,或者使用其他输入动作的输出值,来确定是否激活输入动作。

UE成长之路,欢迎大家的关注! 5464

机器学习势能(MLPs) 第一章 基础理论与核心概念 第二章 数据生成与训练集构建

密度泛函理论(DFT)作为现代计算材料科学的基石,通过求解Kohn-Sham方程在电子结构层面提供可靠的势能面信息。然而,其计算复杂度随体系电子数呈O(N3) 至O(N4) 增长,对于包含数百个原子的体系,单点计算往往需消耗数小时至数天的计算资源。这一瓶颈严重限制了分子动力学模拟的时间尺度,传统DFT-MD通常仅能覆盖皮秒量级,远不足以观测扩散、相变等慢动力学过程。经验力场通过解析函数形式描述原子间相互作用,实现了经典力学的计算效率,但其参数化过程高度依赖物理直觉与经验调参。

VectorShift的博客 182

PTA Isomorphic详细题解

Two trees, T1 and T2, are isomorphic if T1 can be transformed into T2 by swapping left and right children of (some of the) nodes in T1. For instance, the two trees in Figure 1 are isomorphic because t...

PERCENTILE__的博客 1629

【WRF安装】WRF编译错误:problems building executables look for error in the build log

WRF编译错误:problems building executables look for error in the build log

WW、forever的博客 752

4-2 Isomorphic (7分)

4-2Isomorphic(7分) Two trees,T1andT2, areisomorphicifT1can be transformed intoT2by swapping left and right children of (some of the) nodes inT1. For instance, the two trees in Figure 1 ar...

kaggle expert,全球排名前1000,清华计算机研究生,兴趣算法工程 1643

【Flink】FLink session yarn kerberos认证报错 PortUnreachableException: ICMP Port Unreachable

本篇博客是在上一篇博客:【Flink】FLink session yarn kerberos认证报错 KrbException: Cannot locate default realm还没找到原因的时候做的一次尝试。因为项目需要,我们需要对接华为认证的yarn集群,运行flink任务,我们的任务分为两种,yarn pre-job模式和flink session模式。我们在配置文件中配置如下# kerberos认证需要的东西提交任务的脚本如下。

九师兄 1267

RAP 语义键:让 UUID 世界变得可读、可导航、可校验

RAP 语义键的核心价值与实现要点 语义键(Semantic Key)是 ABAP RAP 模型中连接技术实现与业务体验的关键设计:通过为实体指定一组业务可读的标识字段(如订单号、物料编码等),在保持技术主键(如UUID)不变的同时,显著提升用户界面的可读性和导航体验。 主要优势: 提升UI可读性:Fiori元素会突出显示语义键字段,帮助用户快速识别关键业务数据 改善导航体验:OData URL会使用可读的语义键而非技术ID,便于分享和排查问题 降低沟通成本:业务人员能准确引用有意义的业务标识而非技术ID

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 155

4-11 Isomorphic(判断两树同构)

Two trees, T1 and T2, are isomorphic if T1 can be transformed into T2 by swapping left and right children of (some of the) nodes in T1. For instance, the two trees in Figure 1 are isomorphic because they are the same if the children of A, B, and G, but not

wyx_601的博客 1470

YOLOv9改进策略目录 | 包含卷积、主干、检测头、注意力机制、Neck上百种创新机制

Hello,各位读者们好本专栏自开设两个月以来已经更新改进教程50余篇其中包含RepNCSPELAN4、主干、检测头、注意力机制、Neck多种结构上创新,也有损失函数和一些细节点上的创新。同时本人一些讲解视频和包含我所有创新的YOLOv9文档并不能在优快云上传(所有的创新点都经过我的测试是可用的,得到该文件之后大家可以随意组合使用),所以会建立群的形式在内上传我的文件和视频我也会在群内不定期和大家交流回答大家问题,同时定期会更新一些文章的创新点(经过我融合测试后的,先到先得)。

Snu77的博客 1万+

4-11 Isomorphic (10 分)

4-11Isomorphic(10分) Two trees,T1andT2, areisomorphicifT1can be transformed intoT2by swapping left and right children of (some of the) nodes inT1. For instance, the two trees in Figure 1 ...

韩旭051的博客 1930

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Q9.3 Find the magic index

Q: A magic index in an array A[1...n-1] is defined to be an index such that A[i] = i. Given a sorted array of distince integers, write a methon to find a magic index, if one exits, in array A。 FOLLOW

xinxinxv的专栏 1766

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Q9.9 N queens

Q: Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal. A: 经典的8皇后问题。DFS #include #include #include using

xinxinxv的专栏 832

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Q1.8 Check if s2 is a rotation of s1

Q: Assume you have a method isSubstring which checks if one word is asubstring of another. Given two strings, s1 and s2, write code tocheck if s2 is a rotation of s1 using only one call to isSubstring

xinxinxv的专栏 823

1.1 Implement an algorithm to determine if a string has all unique characters.

Implement an algorithm to determine if a string has all unique characters.What if you can not use additional data structures? 思路:首先,字符串是ascii编码还是unicode编码,这会影响记录的数组。  用bool ch[256] 记录所有的字符,初始化为fa

xinxinxv的专栏 720

Q3.1 Describe how you could use a single array to implement three stacks

Q:Describe how you could use a single array to implement three stacks A: 思路一:比较直观的想法是将一个数组分为三部分。将数组平分三部分, 每个部分维护一个栈顶指针。对特定的栈进行操作只要用栈顶指针加上偏移量就可以。 思路二:思路一可能会浪费大量的空间。所以不均分数组为三部分。定义一种数据类型,可以记录当前元素值和上一个

xinxinxv的专栏 706

Q5.4 ((n & (n-1)) == 0).

Q:Explain what the following code does: ((n & (n-1)) == 0). A: 其作用是判断n是否为0 或者是2的整数幂 因为如果n为2的m次幂的话,那么其二进制表示中只有第m位为1,其余全部为0(从0位开始)

xinxinxv的专栏 668

Q5.7 Find the missing number

Q: An array A[1…n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A a

xinxinxv的专栏 666

Q4.2 Given a directed graph, design an algorithm to find out whether there is a route.

Q: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. A: 采用bfs, 同时利用哈希表保存当前节点是否已经被访问了。 如果节点i 和节点j 相连,并且j没有被访问过,那么节点j入栈,说明节点j可能是所求路径上的点。 就这样遍历,直到栈为空或

xinxinxv的专栏 649

Q1.7 set Matrix zeroes

Q:Write an algorithm such that if an element in an MxN matrix is 0,its entire row and column is set to 0. A: 通过遍历,可以用(m+n) 哈希表来储存每一列和每一行是否为0, 再根据哈希表的记录,再次遍历置0; 为了节省空间,可以在上面的思路基础之上进行优化: 1、首先遍历矩阵的第一行

xinxinxv的专栏 612

Q5.8 DrawHorizontalLine

Q: A monochrome screen is stored as a singal array of bytes, allowing eight consecutive pixels to be stored in one byte. The screen has width w, where w is a divisible by 8 (that is, no byte will be s

xinxinxv的专栏 607

Q5.3 Print the next largest and smallest number。 二进制表示中1的个数统计

Q:Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation. A: 思路1:暴力搜索。统计数字的二进制表示中1的位数。统计方法参考http://blog.csdn.net/steedho

xinxinxv的专栏 584

Q3.5 Implement a MyQueue class which implements a queue using two stacks.

Q:Implement a MyQueue class which implements a queue using two stacks. A:一个栈sin用于进,一个栈sout用于出。 push:直接push进sin即可 pop: 如果sout不空,直接pop,否则将sin中的元素都压入sout之后再sout.pop front: 同pop,只不过不pop,而是top back:同f

xinxinxv的专栏 583
上一篇: Q4.7 Find commen ancestor
下一篇: Q4.9 Find all paths which sum to a given value
「已注销」
博客等级 码龄15年 5粉丝 218原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值