Ch4.4: array to Balanced tree and print each level

印尼稻米收成与粮食安全数据集2026年 本数据集收录印度尼西亚2026年水稻收成与粮食安全遥测数据,包含10000条稻米农业收成产量与粮食安全监测记录,围绕印尼国家粮食安全体系中的核心作物水稻展开。稻米是印尼逾两亿人口的主粮,其收成波动直接影响国家粮食安全与进口政策,本数据集将种植区域、收成产量、气候与土壤相关遥测指标整合为结构化表格,可支撑粮食产量预测、收成影响因素分析、区域粮食安全评估与农业政策模拟等研究任务。典型应用场景包括:农业时间序列产量预测模型构建、气候因子与水稻收成的关联分析、省份与地区层面的粮食自给率测算、粮食安全风险预警指标设计,也可作为农业经济、粮食政策与数据科学交叉课程的教学素材。数据为2026年最新版,规模约1.2MB、单CSV结构规整,字段命名清晰,适合Python、Pandas、Scikit-learn等主流工具链直接使用,便于开展探索性分析、可视化与机器学习全流程练习。重要说明:本数据为遥测整理数据,分析结论以数据集文档口径为准,农业决策请结合官方统计综合研判。许可:CC0公有领域,可自由使用与二次分发。 立即下载

Q: an ordered array is given, need to convert into a balanced binary tree and then BFSly print each level as a D-dimentional linked list

Need to understand list, vector, BFS, recursion, scope of member function, call-by-reference, class, template.

The main method is the linked list form processing.

1: push the given head into the list, then push it into a vector.

2. if the vector is not empty, push all the left/right children of the head into a temporary list, after BFS this level, push the temp list into the previous vector as output.


Here is the source code and output.

// Ch4.4: Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).

// based on Hawstein's solution
// and zyli's github

#include 
#include 
#include 

using namespace std;

class BinNode{
public:
    int key;
    BinNode *parent, *lChild, *rChild;
	BinNode(){}
	BinNode(int t): key(t), parent(NULL), lChild(NULL), rChild(NULL){}
};

class BTree{
private:
	BinNode *m_root;
public:
	BTree(): m_root(NULL){}
	BTree(BinNode *t): m_root(t){}

	BinNode* getRoot() const{return m_root;}
	
	BinNode* balanceTree(BinNode* pnode, BinNode *parent, int *arr, int low, int high){
		if(low>high || arr == NULL)
			return NULL;
		int mid = (low+high)>>1;
		pnode = new BinNode(arr[mid]);
		pnode->parent = parent;
		pnode->lChild = balanceTree(pnode->lChild, pnode, arr, low, mid-1);
		pnode->rChild = balanceTree(pnode->rChild, pnode, arr, mid+1, high);

		return pnode;
	}

	void PrintPre(BinNode *pnode)
	{
		if(pnode == NULL)
			return;
 
		cout << pnode->key << " ";
		PrintPre(pnode->lChild);
		PrintPre(pnode->rChild);		
	}

    vector> BFSlist(BinNode *head){
        vector> res;
    	int level = 0;
    	list li;
    	li.push_back(head);
    	res.push_back(li);
    	while(!res[level].empty()){
    		list l;
    		list::iterator it;
    		for(it=res[level].begin(); it!=res[level].end(); ++it){
    			BinNode *n = *it;
    			if(n->lChild) l.push_back(n->lChild);
    			if(n->rChild) l.push_back(n->rChild);
    		}
    		++level;
    		res.push_back(l);
    	}
    	return res;
    }

    void print(vector > res){
        vector >::iterator vit;
        for(vit=res.begin(); vit!=res.end(); ++vit){
            list li = *vit;
            list::iterator lit;
            for(lit=li.begin(); lit!=li.end(); ++lit){
                BinNode *n = *lit;
                cout<key<<" ";
            }
            cout<balanceTree(bt->getRoot(), NULL, arr, low, high);
    bt = new BTree(proot);
    bt->PrintPre(bt->getRoot());
    cout << "\nD-dimesion linked list showoff-------\n";
    vector> jerry = bt->BFSlist(proot);
    bt->print(jerry);
}

/////////////////////////////////////////////////////////////////////////////
/*output:
Executing the program....
$demo 
Balanced tree in array format
10 2 1 5 7 15 13 21 22 
D-dimesion linked list showoff-------
10 
2 15 
1 5 13 21 
7 22 
*/


企业数字化转型AI大模型赋能智能体平台及AI场景应用规划方案(生态合作版).pptx 立即下载

相关推荐

智慧环保生态保护行业可信数据空间应用规划方案(技术方案版).pptx

智慧环保生态保护行业可信数据空间应用规划方案(技术方案版).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

气象服务行业可信数据空间应用规划方案(技术方案版).pptx

气象服务行业可信数据空间应用规划方案(技术方案版).pptx

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

MCGS的Http协议驱动,支持http协议

硬件配置 参数设置 设备属性 设备命令 通讯状态 附录 #HTTP 1、 设备简介 驱动类型 该驱动为独立设备,直接添加构件即可,不需要挂载到父设备下 通讯协议 采用标准HTTP1.1协议 通讯方式 驱动构件为客户端。 2、 硬件连接 本软件在通讯之前,请保证通讯连接正确。通讯连接方式:采用标准的RJ45接口的以太网接线(对调网线或使用HUB交换)。 3、 设备通讯参数 本设备为独立设备,无父设备参数 4、 设备构件参数设置 “HTTP设备”参数设置如下: <1> 内部属性: 构建url信息。 <2> 最小采集周期:本软件对设备进行操作的时间周期,单位为ms,默认为100ms。 <3> 通讯等待时间:通讯数据接收等待时间,默认设置为200ms,当采集速度要求较高或数据量较大时,设置值可适当减小或增大。 <4> HttpHead:http head添加需要的字段,不需要添加http head时值为空,默认不添加。 5、 设备命令 本设备构件提供设备命令,用于进行相应的操作,设备命令的格式如下: 设备命令 命令介绍 GetHttpHead 格式1 GetHttpHead(headStr) 用途 获取http head参数 参数 headStr 参数可以为字符型变量、字符串常量 例1 !SetDevice(设备0,6,"GetHttpHead(headStr)") 获取http head数据保存到headStr中 SetHttpHead 格式1 SetHttpHead(headStr) 用途 设置http head参数 参数 headStr 参数可以为字符型变量、字符串常量

Code Ocean 上传代码 手把手教程

对于一些学术期刊,录用前会让把代码上传到Code Ocean上,但上传过程可能会遇到一些问题,小编分享一下自己的上传经验。他们会进行验证,验证通过,将自动发布。点此运行即可,但是运行比较慢。

u014071947的博客 1万+

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

SuperMap iObjects Java组件开发——环境部署(IntelliJ IDEA)

SuperMap iObjects Java 是面向GIS应用系统开发者的组件式GIS开发平台,具有强大的地理数据管理、编辑、显示、分析等功能,并且其具有极强的易用性和丰富的帮助资源,适宜快速开发大型GIS应用系统。本文将介绍如何在IntelliJ IDEA集成开发环境中进行SuperMap iObjects Java组件的环境部署。

nan620403的博客 1082

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

Typora绘制思维导图

我们观察一下,其实思维导图就是树状结构的数据嘛,作为程序员,拿语言写树状结构的数据是很平常的事情。而且写代码可比画图省事多了,查阅资料可以知道,有一种`mermaid`语法,就是做这件事情的,支持 UML,甘特图,流程图等。

Peter Cheung的博客 7934

UE5深度图与Mask图渲染实战:从基础到高级应用

本文详细解析了在UE5中渲染深度图与Mask图的完整实战流程。从启用电影渲染队列、配置自定义深度模板通道等前期准备,到构建即用型深度图材质网络与精准物体Mask生成,并深入探讨了二者结合在NPR风格化、程序化雾气及AI工作流(如ComfyUI)中的高级应用,为影视、游戏后期与AI创作提供了高效的解决方案。

weixin_29001419的博客 350

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

水文模型新手必看:用SWAT-CUP做参数率定,如何正确设置预热期、率定期和验证期?(附数据准备避坑指南)

本文详细介绍了使用SWAT-CUP进行水文模型参数率定的完整流程,重点解析了预热期、率定期和验证期的正确设置方法,并提供了数据准备的避坑指南。通过实操步骤和常见问题解决方案,帮助新手快速掌握SWAT-CUP参数率定的核心技巧,提升水文模型模拟精度。

weixin_26875109的博客 377

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

​​WPF入门与XAML基础:从零开始构建你的第一个WPF应用​

本文是WPF入门系列首篇,详细介绍WPF的核心优势与开发环境搭建。通过创建第一个WPF项目,解析MainWindow.xaml和App.xaml等关键文件,深入讲解XAML基础语法,包括属性赋值、命名空间和事件绑定。最后通过按钮点击交互示例演示WPF应用程序运行流程,帮助开发者快速上手WPF开发,为后续学习布局系统和数据绑定等高级主题奠定基础。

Hare的博客 3307

认知建模:模拟人类决策过程的AI方法

认知建模的目的在于构建能够模拟人类认知和决策过程的模型,通过AI技术将人类的思维、判断和选择等过程以数字化的形式呈现出来。其范围涵盖了多个领域,包括心理学、人工智能、经济学、管理学等。在心理学领域,认知建模可以帮助研究人员更好地理解人类的认知机制;在人工智能领域,可用于开发具有更智能决策能力的系统;在经济学和管理学中,能辅助进行市场预测、战略规划等决策制定。本文首先介绍认知建模的背景知识,让读者对该领域有初步的了解。接着阐述核心概念和它们之间的联系,通过示意图和流程图直观展示。

AI 算法实战派 1165

复习一下dfs和bfs 用java

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

纯黑老白的专栏 1191

YOLOV12进阶:MSDA多尺度空洞注意力机制解析与实战,突破检测尺度瓶颈

在目标检测中,“尺度差异”是个老大难问题——大目标占据画面大半,小目标可能只有几个像素,普通模型很难同时精准检测。而MSDA(Multi-Scale Dilated Attention,多尺度空洞注意力)就像给模型配备了“变焦眼镜”,能同时关注不同尺度的目标,让YOLOV12在大、中、小目标检测上都能表现出色。这篇教程会带你从原理到实操,掌握这个强大的注意力机制,让你的检测模型真正“通吃”各种尺度的目标。MSDA的“多尺度空洞注意力”设计,是目标检测突破尺度瓶颈的关键一步。

know_F0727的博客 264

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

复习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

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
上一篇: Ch4.3: transform an ordered array into a balanced Tree
下一篇: Ch4.5: find the (in-order tranversely) successor of a node in BST.
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值