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

网络安全实战:从Wireshark到BurpSuite的抓包技巧解析 本文深入解析了Wireshark和BurpSuite在网络安全抓包中的实战技巧,从基础对比到高级过滤、核心模块应用及安全测试避坑指南。通过详细的操作步骤和案例分析,帮助读者掌握数据包抓取与分析的核心技能,提升网络安全防护能力。 阅读详情

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


UCIE Specification详解(二) UCIe架构概述。 阅读详情

相关推荐

HCIA-Storage 存储工程师学习笔记——1. 存储技术趋势

文章目录1. 存储技术趋势1.1 数据、信息与数据存储1.2 存储技术的发展①存储架构的发展②存储介质的发展③ 接口协议的发展1.3 存储产品的发展趋势 华为HCIA-Storage认证地址:https://e.huawei.com/cn/talent/#/cert/roadmap 1. 存储技术趋势 1.1 数据、信息与数据存储 什么是数据? ​ 数据是对所有事物的数字表示。 数据的种类? 结构化数据 半结构化数据 非结构化数据 数据的处理周期 ​ 数据处理是人或机器对数据进行的重组或重新排序,

超级大洋葱的博客 2846

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

读书笔记 | 财务会计理论(第7版 William R.Scott)(中):契约视角下的会计信息价值

本文从契约理论视角深入剖析了财务会计信息的核心价值。文章指出,会计信息作为关键的“契约润滑剂”,通过债务合同与薪酬合同等具体形式,有效解决了公司内外的信息不对称与道德风险问题,协调了股东、债权人及管理层等多方利益,是公司治理与资本市场有效运转的基石。

mac99的博客 330

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

基于树莓派5的AR头显开发:从硬件选型到Unity整合全解析

增强现实(AR)技术通过将虚拟信息叠加到真实世界,为用户提供沉浸式交互体验。其核心原理依赖于计算机视觉进行环境感知与空间定位,并结合实时3D渲染实现虚实融合。在工程实践中,嵌入式平台因其低成本、低功耗特性,成为探索AR应用边界的重要载体。本项目以树莓派5为核心,通过集成OpenCV进行ArUco标记识别实现精准空间定位,并利用MediaPipe完成手势交互,最终在Unity引擎中完成虚实场景的实时渲染与融合,为开发者提供了一个完整的、可亲手实践的AR系统开发方案。

weixin_32430941的博客 254

WorkBuddy结合DeepSeek与Skill:文献检索自动化与Token成本控制实战

Agent工具与大模型API的结合正重塑知识工作者的日常流程。在实际工程中,调用大模型处理批量任务常面临Token消耗过快、成本不可控的痛点,而国产大模型DeepSeek以极低的API定价和兼容OpenAI接口的特性,为规模化文本处理提供了“Token自由”的可能。同时,可复用的Skill机制能够将特定领域的工作流固化为标准化步骤,让Agent自动执行检索式生成、文献筛选与摘要归纳等复杂操作。本文从这一技术背景出发,以科研文献检索为应用场景,演示如何利用WorkBuddy、DeepSeek API与自建文献

weixin_29061509的博客 492

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

jlink v9刷固件修改序列号来修复弹窗

jlink v9弹窗警告修复j

weixin_40500627的博客 9365

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

十年芯片资深IC设计工程师面经系列(四)SDC约束

set_driving_cell:当我们的设计是模块级(Block level)的design时,我们的设计前面一般还有其他的设计,那么前面模块的输出便是我们模块的输入,5.1set_false_path:可以单独的时钟或者路径进行约束。set drive :当我们的设计是芯片级(Chip level)的顶层设计时,芯片外围电路要驱动的是负载较大的IO单元,故需要的驱动能力较强,:set_load 是指output端口的负载,设置的值会影响输出到端口的net上的电容,从而影响相应的路径延时。

weixin_47349046的博客 1773

以 Joule 驱动的生成式开发:把需求文档直接变成可运行的 SAP 应用

摘要:SAP BTP的AI助手Joule正在改变开发方式,支持从自然语言描述自动生成数据模型、服务、UI及测试代码。在SAP Business Application Studio中,Joule能根据业务需求生成结构化代码,并提供代码补全、重构、解释等功能。开发者需注意避免上传敏感数据,并对AI生成的代码进行人工审核。Joule特别针对ABAP开发进行了优化,提供符合SAP规范的代码建议,帮助迁移遗留系统。此外,它还能快速生成CAP项目原型,但所有AI生成内容都需经过严格测试验证。

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

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

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

Q1.4 Write a method to replace all spaces in a string with ‘%20’.

思路: 1、遍历, 原来的字符串长度len, 记录空格个数cnt, 则最后的字符串长度len+2*cnt。 2、从后往前遍历,遇到空格将其替换为"%20",否则原样拷贝 #include #include using namespace std; void replace(char *c){ if(c == NULL) return; int len = strlen(

xinxinxv的专栏 578
上一篇: 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、付费专栏及课程。

余额充值