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

SpringBoot项目实战:5分钟搞定豆包大模型API接入(附完整代码) 本文详细介绍了如何在SpringBoot项目中快速集成豆包大模型API,仅需5分钟即可完成。从环境准备、依赖配置到核心服务封装和控制器层实现,提供了完整的代码示例和实战技巧,帮助开发者高效接入大模型能力,提升智能客服等应用的开发效率。 阅读详情

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;
}


基于IMX429-IMX430-IMX432-IMX437系列传感器的SLVS-MIPI桥接方案设计与实现 本文详细介绍了针对索尼IMX429、IMX430、IMX432及IMX437系列传感器的SLVS-MIPI桥接方案设计与实现。针对工业视觉、车载摄像头等应用中主流SoC仅支持MIPI接口的痛点,提出基于FPGA(如Lattice Crosslink-NX)的透传桥接方案,将传感器输出的SLVS信号无损转换为MIPI CSI-2信号,确保数据完整性与低延迟,并探讨了硬件选型、逻辑设计及多传感器兼容性等关键问题。 阅读详情

相关推荐

SAP ABAP实战:手把手教你用BAPI_BILLINGDOC_CREATEMULTIPLE批量开票(附完整代码与避坑指南)

本文详细解析了SAP ABAP中BAPI_BILLINGDOC_CREATEMULTIPLE批量开票的全流程,包括参数配置、事务控制、错误处理及性能优化。通过实战案例和完整代码示例,帮助开发者高效处理大批量发票生成任务,避免常见陷阱,提升开发效率。

weixin_34101914的博客 266

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

微信DAT图片解密全攻略:一键转换JPG/PNG/GIF

本文详细解析了微信电脑版中DAT图片的加密原理与存储路径,并提供了从定位文件到使用专业工具进行一键解密的完整操作指南。通过简单的步骤,用户可将加密的DAT文件无损还原为JPG、PNG或GIF格式,有效解决无法直接查看聊天图片的难题,同时兼顾了隐私清理与磁盘空间管理。

hill8的博客 1363

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

【数字逻辑电路课程设计】多功能数码管时钟VHDL+quartus II-EP1C12F324C8+源码+付费资源

我希望需要我这篇文章的是正常学习数字逻辑课程且正在设计数码管时钟的同学。我个人不推荐直接获取源代码,因此我另有一份付费资源是完整源代码;我推荐各位去研究我代码的逻辑尝试自己书写属于自己的代码,因此我保留了注释但隐去了变量。由于时间有限,我的设计不够完美,仍有很多缺陷,我将我的展望写在后面,各位可以尝试实现。我不为难同学,作为同学我理解各位设计的辛苦,因此如果有需要,私信我提供隐去敏感信息的学生证照片可直接获取源代码。

WannaFlysyx的博客 1534

基于51单片机的简易计算器设计(原理图+仿真+源代码+参考论文)

本设计以51单片机为核心,矩阵键盘输入数字,通过1602显示屏显示计算结果,能够实现整数的加、减、乘、除四则运算,具备清除功能。

u012759563的博客 7698

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

Containerd容器运行时的私有仓库镜像推送push和镜像拉取问题解决

向/etc/containerd/config.toml配置添加如下配置,然后重启containerd。向/etc/containerd/config.toml配置添加如下配置,然后重启containerd。通过crictl为k8s拉取镜像如下所示。可以通过ctr命令进行镜像推送。往私有仓库推送镜像如下。

Bulut0907 1万+

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

Unity 第一人称视角控制移动、旋转

第一人称视角操作此功能主要是借鉴YouTube,写的蛮好我就搬运过来让大家也试试。

qq_36515472的博客 4202

工业视觉实战:用摄像头+AI高精度测量钢板尺寸(附深度分析)

摘要: 钢铁制造业正加速采用基于摄像头与深度学习的视觉测量技术,以取代传统人工测量,实现钢板长、宽、高(厚)的非接触、高精度在线检测。核心方案包括单目视觉(低成本但受限于变形)、双目/多目立体视觉(可精确测量变形钢板)及3D激光扫描(微米级精度)。系统精度受硬件(相机分辨率、镜头)、算法(亚像素技术)及环境稳定性共同影响,综合精度可达±0.1mm。实战中需重点解决热膨胀(需红外测温实时补偿)和钢板变形(必须采用三维方案)两大挑战,并通过工程化设计(散热、防振)确保系统可靠运行。该技术需融合光学、机械与算法,

数字人生 391

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

【功能安全】软件安全架构

目录01 软件安全架构介绍02 软件架构设计模板03 软件架构设计示例 软件架构设计的目的 软件架构设计的原则 软件架构设计输入与输出 软件架构设计的要求 注:软件架构设计和软件安全要求之间的双向可追溯性。软件架构设计原则: 软件架构静态/动态设计(参考秒尼科技术) 软件架构静态设计 软件架构动态设计 软件分层架构设计 软件架构设计安全机制错误探测的安全机制 其中:数据的范围检查指输入输出数据的范围进行检查,例如:在程序中我们可以通过数据范围来排除数据的正确性;合理性检查 例如:使用参

weixin_36460584的博客 511

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

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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、付费专栏及课程。

余额充值