Ch4.8: find all the path that a Binary tree sums up to a given value, may/maynot have parent pointer

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

In this problem, we should consider the path to the bottom, instead of hit-and-run. As a result, the recursive starts summation from the children up to their parents not from up to down.

For example:

The given sums up value is 8:

then these are all correct paths:

8;

8=-2=2;

2=0=6;

2=0=6=-1=1;


Here is the full code. Very well design.

//http://leetcode.com/2010/09/how-to-pretty-print-binary-tree.html

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct BinaryTree {
  BinaryTree *parent, *left, *right;
  int data;
  BinaryTree(int val) : parent(NULL), left(NULL), right(NULL), data(val) { }
};

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

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

        return pnode;
    }

    void PrintPre(BinaryTree *pnode)
    {
        if(pnode == NULL)
            return;
 
        cout << pnode->data << " ";
        PrintPre(pnode->left);
        PrintPre(pnode->right);        
    }


// Find the maximum height of the binary tree
int maxHeight(BinaryTree *p) {
  if (!p) return 0;
  int leftHeight = maxHeight(p->left);
  int rightHeight = maxHeight(p->right);
  return (leftHeight > rightHeight) ? leftHeight + 1: rightHeight + 1;
}

// Convert an integer value to string
string intToString(int val) {
  ostringstream ss;
  ss << val;
  return ss.str();
}

// Print the arm branches (eg, /    \ ) on a line
void printBranches(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque& nodesQueue, ostream& out) {
  deque::const_iterator iter = nodesQueue.begin();
  for (int i = 0; i < nodesInThisLevel / 2; i++) {
    out << ((i == 0) ? setw(startLen-1) : setw(nodeSpaceLen-2)) << "" << ((*iter++) ? "/" : " ");
    out << setw(2*branchLen+2) << "" << ((*iter++) ? "\\" : " ");
  }
  out << endl;
}

// Print the branches and node (eg, ___10___ )
void printNodes(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque& nodesQueue, ostream& out) {
  deque::const_iterator iter = nodesQueue.begin();
  for (int i = 0; i < nodesInThisLevel; i++, iter++) {
    out << ((i == 0) ? setw(startLen) : setw(nodeSpaceLen)) << "" << ((*iter && (*iter)->left) ? setfill('_') : setfill(' '));
    out << setw(branchLen+2) << ((*iter) ? intToString((*iter)->data) : "");
    out << ((*iter && (*iter)->right) ? setfill('_') : setfill(' ')) << setw(branchLen) << "" << setfill(' ');
  }
  out << endl;
}

// Print the leaves only (just for the bottom row)
void printLeaves(int indentSpace, int level, int nodesInThisLevel, const deque& nodesQueue, ostream& out) {
  deque::const_iterator iter = nodesQueue.begin();
  for (int i = 0; i < nodesInThisLevel; i++, iter++) {
    out << ((i == 0) ? setw(indentSpace+2) : setw(2*level+2)) << ((*iter) ? intToString((*iter)->data) : "");
  }
  out << endl;
}

// Pretty formatting of a binary tree to the output stream
// @ param
// level  Control how wide you want the tree to sparse (eg, level 1 has the minimum space between nodes, while level 2 has a larger space between nodes)
// indentSpace  Change this to add some indent space to the left (eg, indentSpace of 0 means the lowest level of the left node will stick to the left margin)
void printPretty(BinaryTree *root, int level, int indentSpace, ostream& out) {
  int h = maxHeight(root);
  int nodesInThisLevel = 1;

  int branchLen = 2*((int)pow(2.0,h)-1) - (3-level)*(int)pow(2.0,h-1);  // eq of the length of branch for each node of each level
  int nodeSpaceLen = 2 + (level+1)*(int)pow(2.0,h);  // distance between left neighbor node's right arm and right neighbor node's left arm
  int startLen = branchLen + (3-level) + indentSpace;  // starting space to the first node to print of each level (for the left most node of each level only)

  deque nodesQueue;
  nodesQueue.push_back(root);
  for (int r = 1; r < h; r++) {
    printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out);
    branchLen = branchLen/2 - 1;
    nodeSpaceLen = nodeSpaceLen/2 + 1;
    startLen = branchLen + (3-level) + indentSpace;
    printNodes(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out);

    for (int i = 0; i < nodesInThisLevel; i++) {
      BinaryTree *currNode = nodesQueue.front();
      nodesQueue.pop_front();
      if (currNode) {
          nodesQueue.push_back(currNode->left);
          nodesQueue.push_back(currNode->right);
      } else {
        nodesQueue.push_back(NULL);
        nodesQueue.push_back(NULL);
      }
    }
    nodesInThisLevel *= 2;
  }
  printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out);
  printLeaves(indentSpace, level, nodesInThisLevel, nodesQueue, out);
}


void printREV(BinaryTree* head, int level){
    vector v;
    for(int i=0; idata);
        head = head->parent;
    }
    while(!v.empty()){
        cout<balanceTree(bt2->getRoot(), NULL, a2, low2, high2);
    bt2 = new BTree(proot2);
    bt2->PrintPre(bt2->getRoot());
    cout << "Tree pretty print with level=1 and indentSpace=0\n\n";
  // Output to console
  bt2->printPretty(proot2, 1, 0, cout);
  
  
  // 
  int sum = 8;
  cout << "\nall the paths sums to: "<< sum<find_sum(proot2, sum);
  
  
  cout << "if Struct BinaryTree doesn't have parent pointer.........\n";
  vector v_additional;
  bt2->find_sum2(proot2, sum, v_additional, 0);
  return 0;
}

/*
Executing the program....
$demo 
a2:
0----------13
5 8 4 3 -2 0 2 2 -3 3 4 0 1 6 Tree pretty print with level=1 and indentSpace=0


        _______5______
       /              \
    ___8__          ___2__
   /      \        /      \
   4      -2      -3      _0
    \    /  \    /  \    /  \
     3   0   2   3   4   1   6

all the paths sums to: 8
if Struct BinaryTree has parent pointer.........
8 
8 -2 2 
5 2 -3 4 
5 2 0 1 
2 0 6 
if Struct BinaryTree doesn't have parent pointer.........
8 
8 -2 2 
5 2 -3 4 
5 2 0 1 
2 0 6 
*/


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

DataHub 架构设计与核心工作原理

DataHub 是一个第三代数据目录系统,专为现代数据栈设计,支持数据发现、协作、治理和端到端可观测性。其架构采用模型优先的设计理念,注重不同工具和系统之间的互操作性。

北斗云的博客 1227

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

【C++】排序算法

冒泡排序、插入排序、选择排序、希尔排序、堆排序、归并排序、快速排序、计数排序、基数排序、桶排序、外排序

phoenixFlyzzz的博客 2069

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

1、Ovirt 开源虚拟化平台安装

Ovirt虚拟化Node和Engine安装

大鱼 6267

路由工具之策略路由policy-based-routing、MQC三板斧流分类、流行为、流策略

PBR(Policy-Based Routing,策略路由)策略路由可理解为控制路由,因为PBR策略的优先级高于传统路由表的转发,所以它主要用于影响数据包转发的技术。

Hades_Ling的优快云博客 5583

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

Windows加域迁移避坑指南:Profwiz工具实战中的5个常见问题与解决方案

本文详细解析了使用Profwiz工具进行Windows加域迁移时的5个常见问题及解决方案,包括临时配置文件生成、密码策略冲突、SID冲突等。通过实战案例和优化技巧,帮助IT运维团队高效完成账户配置文件迁移,确保Active Directory环境下的数据完整性和权限无缝衔接。

q9r0s的博客 525

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

ZYNQ裸机LWIP双网口实现

文章目录前言一、LWIP双网口二、使用步骤1.部分说明2.部分代码总结 前言 一、LWIP双网口 由于项目需要多通讯口,查阅资料发现lwip支持多网口操作。于是,就开搞了。 二、使用步骤 1.部分说明 利用xemac_add函数在网卡链表中添加一个网卡,即使用netif块重复xemac_add操作。 先用轮询的方式实现双网口,后期修改为中断模式 2.部分代码 导入官方例程LWip echo server, 修改main函数,增加一个netif块 #if LWIP_IPV6==0 #if L..

weixin_35865280的博客 1万+

Arduino、ESP32驱动BMP390L数字大气压传感器(压力传感器)

BMP390L是一款博世新研发的工业级专用数字气压传感器,具有极高的温度稳定性、低漂移、低功耗。

不脱发的程序猿 2016

复习一下dfs和bfs 用java

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

纯黑老白的专栏 1191

【时空图神经网络 & 交通】相关模型3:STFGNN | 空间-时间依赖性 | 基于数据驱动的方法来生成“时间图” | 时空融合图模块,门控卷积模块 | 动态时间规整 | 顶会文章解析

STFGNN(时空融合图神经网络)在交通流量预测中的优势主要体现在以下几个方面:首先,它提出了一种数据驱动的方法生成“时间图”,弥补了传统空间图可能无法反映的某些相关性。其次,STFGNN通过并行处理不同时间段的空间图和时间图,有效地学习隐藏的时空依赖关系。此外,它将融合图模块与门控卷积模块整合到一个统一层中,通过层层堆叠处理长序列,进一步增强了模型的表达能力。实验结果表明,STFGNN在多个交通数据集上表现出色,优于其他基准方法,展示了其在实际应用中的强大潜力。

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 386

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

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

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

怀旧版电脑GBC模拟器

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

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

VC中sqlite的使用

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

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.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one
下一篇: Lesson1: Udacity parallel programming 学习笔记
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值