Ch4-1: identify if a tree is "balanced" (注:CTCI 第四版和第五版的定义不一样)

【高分子材料工程】嵌件注塑结构设计与工艺优化:金属-塑料复合件界面强度及失效预防技术规范 内容概要:本文系统阐述了嵌件注塑(Insert Molding)的设计参数、模具关键技术及失效预防措施,聚焦黄铜H62或304不锈钢与PA66-GF30材料组合的基准工况,提供可用于工程图纸、DFM检查、模具验收和工艺规程的量化依据。内容涵盖工艺边界界定、材料匹配与界面力学分析、结构设计参数(如包覆壁厚、滚花、倒角)、模具设计要点(定位、浇口、排气、冷却)、工艺参数窗口、八大失效模式及其对策、验证验收标准以及量产控制要求,强调通过DFM仿真、模流分析和DOE试模优化工艺,并列出常见设计误区与纠正方案。; 适合人群:从事注塑产品设计、模具开发、工艺工程及质量管理的工程技术人员,尤其适用于汽车、电子、医疗等行业中具备1-3年及以上相关经验的研发人员; 使用场景及目标:①指导嵌件注塑零件的可制造性设计与结构优化;②支持模具设计评审与验收;③制定试模调机与量产工艺规范;④开展失效分析与质量改进; 阅读建议:此资源兼具理论机制与工程实践,建议结合实际项目对照查阅,重点关注包覆壁厚、预热温度、浇口布置等关键参数的取值边界,并在应用非基准材料时进行线膨胀系数与收缩率的换算修正,避免直接套用数据。 立即下载

To identify if the most different leaves, it is:  If < 1, then balance. otherwise, not.

So we need to traverse the tree to find the leaves' depth, so we pick 1 of 3 traverse oders: the inorder.

In order to implement a tree, need a structure:

struct BinNode{
	int data;
	BinNode *parent, *lChild, *rChild;
};

Then we can traverse each node and find the depth of each leaf. Read this snippet, very elegant recursion version (although not high efficient as iteration version since large constant coefficient of O(n)). 

Look at the:  ++d, dep[++numLeaf]=d, --d;

// the helper method
int d=0, numLeaf=0, dep[MAX_NUM];
void getDep(BinNode* head){
	if(head == NULL) return;
	++d;  // key in recursion
	getDep(head->lChild);
	if(head->lChild==NULL && head->rChild==NULL){
		dep[++numLeaf] = d;  //
	}
	getDep(head->rChild);
	--d;  // key in recursion
}

Then we can simply check all the dep[numLeaf] and find the largest, smallest one, and find the difference of them.

Also, as the linked list, we need to implement tree, so we need to initialize it by init() and insert().

The insert() is elegant too.

// very good design of the insert method!
void insert(BinNode* &head, int value){
	if(head==NULL) {
		//head->data = value;  Hawstein used array to store BST
		BST[idx].data = value;
		BST[idx].parent = p;
		head = &BST[idx++]; // note, this is post increment 
		return;  // C++ primer P223, this return equal to break
	};

	p = head;
	if(value < head->data)
		insert(head->lChild, value);
	else
		insert(head->rChild, value);
}

And here is the whole code:

// Ch4-1 solution: identify if a BST is balanced.
// Note: this is the solution for CTCI 4th, not 5th.
// In 4th, the definition of balanced is: a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
// In 5th, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.

#include 
#include 

const int MAX_NUM = 100;

using namespace std;

struct BinNode{
    int data;
	BinNode *parent, *lChild, *rChild;
};

BinNode *head, *p, BST[MAX_NUM];
int idx;

void init(){
	head = p = NULL;
	memset(BST, '\0', sizeof(BST));  // since an array, then init as 
	idx=0;							 // EOF by using termination value
}

void insert(BinNode* &head, int value){
	if(head==NULL) {
		//head->data = value;  Hawstein used array to store BST
		BST[idx].data = value;
		BST[idx].parent = p;
		head = &BST[idx++];
		return;
	};

	p = head;
	if(value < head->data){
		insert(head->lChild, value);
        //cout << "inserted: "<< value << " as lChild of "<< head->data << endl;
    }
    else{
		insert(head->rChild, value);
        //cout << "inserted: "<< value << " as rChild of "<< head->data << endl;
    }
}

// the helper method
int d=0, numLeaf=0, dep[MAX_NUM];
void getDep(BinNode* head){
	if(head == NULL) return;
	++d;  // key in recursion
	getDep(head->lChild);
	if(head->lChild==NULL && head->rChild==NULL){
		dep[++numLeaf] = d;  //
	}
	getDep(head->rChild);
	--d;  // key in recursion
}

// the main method
bool isBalance(BinNode* head){
	if(head==NULL) return true;
	getDep(head);
	int max = dep[0], min = dep[0];
	for(int i =0; imax) max=dep[i];
		if(dep[i] 1) return false;
	else return true;
}

int main(){
	init();
	int a[] = {
		5, 3, 8, 1, 4, 7, 10, 2, 6, 9, 11, 12
        //5, 3, 8, 12
	};
    int num_a = 12;
	for(int i = 0; idata << endl;
    cout << checker;
    return 0;
}

and the output:

Executing the program....
$demo 
0
testing num_a    1
5: BST[0]
3: BST[1]
8: BST[2]
1: BST[3]
4: BST[4]
7: BST[5]
10: BST[6]
2: BST[7]
6: BST[8]
9: BST[9]
11: BST[10]
12: BST[11]
testing num_a    2
data: 10
0x6018a0


智慧医共体AI大模型赋能智能体平台及AI场景应用规划方案(决策评审版).pptx 立即下载

相关推荐

AI政务大模型数字人平台规划方案(生态合作版).pptx

AI政务大模型数字人平台规划方案(生态合作版).pptx

cc150做题计划

计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。 2月底面试前完成200题C++。 这里记录一下ctci学习的参考资料。 1. hawstein的blog 2. https://github.com/ChengTian/CtCI 3. absolute C++ 5th 4. Primer C++ 5. learncpp:http

纯黑老白的专栏 2909

14-15日周末听写音频.mp3

14-15日周末听写音频.mp3

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes: it means: (from leetcode blog: 点击打开链接) _______3______ / \

纯黑老白的专栏 1330

图腾柱无桥PFC变换器+平均电流控制+PI电压双闭环前馈控制设计(Simulink仿真实现)

内容概要:本文系统研究了图腾柱无桥PFC变换器的高效率控制策略,采用平均电流控制结合PI电压双闭环前馈控制,利用Simulink平台构建完整的系统仿真模型,涵盖主电路拓扑、控制环路设计、关键参数计算及动态性能验证。研究重点在于提升功率因数、降低输入电流谐波含量,并确保输出电压的高度稳定性。通过仿真分析,全面评估了系统在启动、稳态运行及抗干扰等方面的性能表现,充分展现了现代电力电子变换器在高效能、高可靠性方面的先进控制技术应用; 适合人群:具备一定电力电子技术基础和Simulink仿真能力的电气工程及相关专业的研究生、科研人员及企业研发工程师; 使用场景及目标:①研究高效率PFC变换器的控制策略;②掌握图腾柱无桥PFC电路的工作原理与建模方法;③开展新能源、电动汽车充电、工业电源等领域中高性能整流器的技术开发与性能优化; 阅读建议:建议读者结合Simulink仿真模型同步操作,重点关注控制环路的设计细节与参数整定过程,深入理解平均电流控制与双闭环前馈控制的协同机制,同时可拓展至不同工况下的性能测试以增强工程实践能力。

PVE(Proxmox VE)添加VMware Esxi并迁移虚拟机

跨平台迁移虚拟机:从VMware虚拟化平台迁移虚拟机到PVE平台。

VEGETA的博客 7720

Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)

这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。 题目在这:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.

纯黑老白的专栏 1295

用Python玩转Iris数据集:从数据加载到可视化分析的完整指南

本文提供了使用Python处理Iris数据集的完整指南,涵盖数据加载、探索性分析、特征工程和可视化呈现等关键步骤。通过Pandas、NumPy、Matplotlib和Seaborn等工具,读者将学习如何从零开始完成一个机器学习项目的数据分析流程,掌握数据科学的核心技能。

j2k3l4的博客 850

Ch2-2: return the nth to the last node data of a singly linked list

In this problem, I need to find the nth to the last node data of a singly linked list.  In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an

纯黑老白的专栏 1290

红外与可见光融合避坑指南:LLVIP数据集实测6大算法效果对比

本文基于专为弱光视觉设计的LLVIP数据集,对GTF、DenseFuse、FusionGAN、IFCNN等6种主流红外与可见光图像融合算法进行了深度实测与对比。通过客观指标与可视化分析,揭示了算法在极端弱光下易出现特征丢失的共性问题,并探讨了融合图像对下游目标检测任务的实际增益,为工程选型与算法调优提供了实用指南。

weixin_29092031的博客 441

告别配置噩梦:用Vcpkg三行命令为Easy3D自动安装Boost、CGAL和Qt(VS2022实测)

本文详细介绍了如何使用Vcpkg工具在VS2022环境下为Easy3D项目一键安装Boost、CGAL和Qt等依赖库,解决传统手动配置的复杂性问题。通过三行简单命令即可自动完成下载、编译和路径配置,大幅提升开发效率,特别适合C++开发者快速搭建3D图形开发环境。

weixin_42525582的博客 213

1/22/2014: 纪念第一个C++ programmer电面--F家

经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。 Anyway,谢谢郭志给的refer,不然连电面的机会都没有。 开头是C++基础题: 1. Fib的2种写法:recursive和DP 2. Struct和Class的区别;在C

纯黑老白的专栏 1274

组态王|以一定的时间间隔查询某个时间段内的历史数据

使用该函数其他都可以满足,唯一问题是变量的选择,当切换站点后查询历史数据时,所选的变量依然是上次站点查询时所选择的变量,每当更换站点查询时都需要重新选择变量,这一点太繁琐,是用户和操作者无法忍受的。但是由于现场可能数据变化并非太过频繁,想要实现记录频率不变,还是以1分钟一次的频率进行存储,但是报表查询的时候想要只显示整点的数据。其中日历控件的设置与之前的历史数据查询是一样的,报表的抬头设置都是不用变的,此处不再赘述,有兴趣的可以。以上为组态王中以一定的时间间隔查询某个时间段内的历史数据的实现过程。

做有价值的事,积累有价值的笔记! 2550

Ch1-7: clear the row i and column j iff a[i][j]==0

in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE. Then the send loop make the TURE row/col

纯黑老白的专栏 1207

五子棋AI算法第一篇-我对谷歌Alphago的看法

最近Google宣布他们的围棋AI以5:0的战绩打败欧洲围棋三冠王。我一直对人工智能比较感兴趣,正好趁年前空闲的几天用JS写了一个五子棋的AI。其实很久以前就用自创的算法写过一个JAVA版的五子棋AI,不过棋力很不理想。这次用的是JS写的,所以无论任何平台上只要能打开网页的都可以玩。google爆出这个新闻之后,很多人觉得这是不是意味着电脑智商已经在人类之上了,围棋已经没有意义了之类的。甚至觉得这是

言川的博客 2万+

饥荒联机版Mod开发——modmain环境与API深度解析

本文深入解析饥荒联机版Mod开发中的modmain.lua环境与核心API,详细讲解环境表构建原理、API注册与调用时机,并提供高级开发技巧与最佳实践。通过源码分析和实际案例,帮助开发者掌握Mod开发核心技术,解决常见问题,提升开发效率与代码质量。

weixin_30312563的博客 437

复习一下dfs和bfs 用java

dfs 和 bfs都是用来找从source 到 target的path的算法。 dfs用的是recursion,bfs则用queue

纯黑老白的专栏 1191

【即插即用模块】注意力篇 | TGRS 2025 | MFA聚特征+HPDown保细节+HFF融层级,三大模块各司其职,性能翻倍!

本文介绍了MTMLNet网络中的三大核心模块:MFA(多阶段特征聚合)、HPDown(混合池化下采样)和HFF(分层特征融合)。MFA通过双尺度深度卷积和注意力机制增强特征提取能力;HPDown结合平均池化和最大池化,有效保留下采样过程中的特征信息;HFF采用三注意力机制实现自适应特征融合。这三个模块协同工作,在红外小目标检测与分割任务中取得了SOTA效果。实验表明,各模块在特征提取、信息保留和融合方面均优于传统方法,可灵活应用于各类视觉任务中。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 287

Ch3-1: use a single array to implement three stacks.

Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici

纯黑老白的专栏 1097

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

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

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one

// Ch4.7: 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 #include #include #includ

纯黑老白的专栏 1030

计算机控制技术及应用

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

复习recursion

1. 如果这样 private void lop(int x, int size, double[] jerry){ if(x==size-1){ jerry[x] = -1; return; } if(jerry[x+1]==0){ jerry[x+1] = jerry[x]- 1; //if so, lop(x+1, size, jerry); }

纯黑老白的专栏 1009

ssm框架搭建

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

Ch2-5: find the beginning of loop in a circular linked list---two solutions

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer poin

纯黑老白的专栏 986

Ch3-2: implement min() for a stack with O(1)

Here will show the thinking flow from bad to good. idea 1: const int MAX_INT = ~(1<<31);//2147483647 struct node{ int val, min; }; class StackWithMin{ public: StackWithMin(int size){

纯黑老白的专栏 956

Ch8.8: 8 queen problem

In order to solve this problem efficiently, need to use backtracking method instead of iterations. Aka: Backtracking is a general algorithm for finding all (or some) solutions to some computational p

纯黑老白的专栏 949

Ch3-3: popat(int idx) of setofstack

Continue to use class to design solution, also, start to use STL, such as container (vector, stack, set, ...). save time and to be more professional. The design without popat method is similar to the

纯黑老白的专栏 948

Ch4-2: find if 2 given vertices are connected in a directed map,BFS + JUST fix the DFS recursion

In this problem, there are 2 ways: BFS or DFS. Hawstein used BFS: #include #include #include #include using namespace std; const int maxn = 100; bool g[maxn][maxn], visited[maxn]; int n;

纯黑老白的专栏 938
上一篇: 二叉树的insertLC
下一篇: Ch4-2: find if 2 given vertices are connected in a directed map,BFS + JUST fix the DFS recursion
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值