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 参数可以为字符型变量、字符串常量

Unity中5自由度机械臂运动控制与夹爪交互仿真实践

本文详细介绍了在Unity中实现5自由度机械臂运动控制与夹爪交互仿真的完整流程。从模型导入、物理组件配置到关节运动控制算法(包括逆运动学IK),再到夹爪抓取逻辑和工业分拣案例实战,提供了可落地的代码示例与性能优化方案,助力开发者快速掌握机械臂仿真核心技术。

weixin_29021757的博客 447

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

MFC 修改CheckBox的文本颜色方法

设置主题 这一步是必须的,不可省略。 BOOL CMFCApplication1Dlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 // 设置主题 HWND

Kim的博客 2249

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

王道考研计算机网络课件全解析:从零搭建你的知识框架(附B站视频+PPT下载)

本文全面解析王道考研计算机网络课程的核心知识体系,从OSI七层模型到TCP/IP四层架构,详细讲解物理层、数据链路层、网络层、传输层和应用层的关键技术。提供高效学习路径、实战案例和常见误区警示,帮助考生系统构建计算机网络知识框架,适合计算机考研备考者参考学习。

leaf8的博客 570

超详细版risc-v五级流水线cpu硬件设计实现解析

深入剖析risc-v五级流水线cpu的硬件架构与设计细节,涵盖指令流程、模块划分与关键时序控制,帮助理解risc-v五级流水线cpu的工作机制与优化策略。

weixin_42437253的博客 1638

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

飞腾X100芯片GPU状态查询

本文档对在linux系统下查看X100芯片GPU状态信息进行说明,可以帮助大家了解芯片的实时工作状态。

乌拉大喵喵 2821

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

Android Audio基础——概览(一)

音频系统使用该属性进行音频焦点控制、音频通道路由及音量控制等,一般在应用开发中常用。mUsage // 指定播放声音原因和作用。用来控制焦点、路由及音量决策(USAGE_MEDIA、USAGE_VOICE_COMMUNICATION、未知等)mContentType // 指定声音来源的类型(CONTENT_TYPE_MOVIE、CONTENT_TYPE_MUSIC、语音、发音、未知等)mSourcemFlags //指定音频框架如何对音频播放应用效果DeviceType。

小旭的专栏 3174

【从零开始学习 UVM】1.2、UVM 概述 —— UVM Installation(UVM 安装)

【从零开始学习 UVM】1.2、UVM 概述 —— UVM Installation(UVM 安装)

努力,加油每一天鸭~ 1947

复习一下dfs和bfs 用java

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

纯黑老白的专栏 1191

OpenCV实战之人脸美颜美型(八)——大眼(上)

从大众审美的角度来看,眼睛适当的“变大”会让人显得更加年轻。因此美型中就有一项大眼功能,其效果就是让眼睛适当变大,这章内容就围绕着大眼算法展开。

On my way 1211

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

余额充值