699 - The Falling Leaves

本文介绍了一种计算二叉树中同一垂直线上节点值之和的方法,通过解析特定输入格式的二叉树并利用栈来区分不同的树,最终输出各垂直线上的节点值总和。



  The Falling Leaves 

Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?


We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the "leaves" drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

Input 

The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value -1 is supplied. Thus the tree shown above is specified as 5 7 -1 6 -1 -1 3 -1 -1. Each actual tree node contains a positive, non-zero value. The last test case is followed by a single -1 (which would otherwise represent an empty tree).

Output 

For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of "leaves" in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the examples below.

Sample Input 

5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1

Sample Output 

Case 1:
7 11 3

Case 2:
9 7 21 15

题意:把在同一垂直线上的树中的结点定义为一个落叶堆,要求求出各个堆上的结点值之和。

关键点1:关键在看懂输入案例,有可能多个行才代表一颗树,输入以-1结束。所以,问题的关键在于把一棵树从输入中区分出来。如何才能把一颗树区分出来了呢?使用一个栈,每输入一个元素先入栈,然后判断输入的值是否为-1,若是从栈中弹出两个元素,若这两个元素都是-1代表当前这个子树结束了,再把它的根结点弹出来,把-1压栈,循环这个操作即可。

关键点2:如何计算各个垂直线上的结点之和?我们可以假设根结点的垂值线坐标为width=0,那么其左子树根结点的坐标为width-1,右子树根结点的坐标为width+1,子树同理...这里其实就树的前序递归遍历的变体

代码:

#include <iostream>
#include <vector>
#include <stack>
#include <map>

using namespace std;

void leaf_heap(map<int,int> &m,vector<int> &v,int n,int &cur,int width)
{
	if(cur>=n || v[cur]==-1 ){
		return ;
	}
	m[width]+=v[cur];
	leaf_heap(m,v,n,++cur,width-1);
	leaf_heap(m,v,n,++cur,width+1);
}

int main()
{
	stack<int > stk;
	vector<int> v;//存储树的输入序列
	int value;
	int case_num=0;
	bool fisrt=true,tree_end=false;;
	while(cin>>value){
	    if(value==-1 && fisrt) break;//第一个,而且值为-1,输入结束
		else {
			fisrt=false;  
		}
		v.push_back(value);
		stk.push(value);
		while(value==-1 && !stk.empty()){
			if(stk.size()==1 && stk.top()==-1){//一棵树结束了
				++case_num;
			    fisrt=true;
				tree_end=true;
				break;
			}
			int x=stk.top();stk.pop();
			int y=stk.top();stk.pop();
			if(x==y && x==-1) {//一个子树结束了
				stk.pop();
				stk.push(-1);
			}else if(y!=-1){
				stk.push(y);
				stk.push(x);
				break;
			}
		}

		if(tree_end){//一颗树的输入结束
			map<int,int> m;
			int cur=0,width=0;
			leaf_heap(m,v,v.size(),cur,width);
			cout<<"Case "<<case_num<<":"<<endl;
			bool first=true;
			for(auto p:m){
				if(first){
					cout<<p.second;
					first=false;
				}else{
					cout<<" "<<p.second;
				}
			}
			cout<<endl;
			cout<<endl;
			stk.pop();
			v.clear();
			tree_end=false;
		}

	}
	return 0;
}

内容概要:本文为《科技类企业品牌传播白皮书》,系统阐述了新闻媒体发稿、自媒体博主种草与短视频矩阵覆盖三大核心传播策略,并结合“传声港”平台的AI工具与资源整合能力,提出适配科技企业的品牌传播解决方案。文章深入分析科技企业传播的特殊性,包括受众圈层化、技术复杂性与传播通俗性的矛盾、产品生命周期影响及2024-2025年传播新趋势,强调从“技术输出”向“价值引领”的战略升级。针对三种传播方式,分别从适用场景、操作流程、效果评估、成本效益、风险防控等方面提供详尽指南,并通过平台AI能力实现资源智能匹配、内容精准投放与全链路效果追踪,最终构建“信任—种草—曝光”三位一体的传播闭环。; 适合人群:科技类企业品牌与市场负责人、公关传播从业者、数字营销管理者及初创科技公司创始人;具备一定品牌传播基础,关注效果可量化与AI工具赋能的专业人士。; 使用场景及目标:①制定科技产品全生命周期的品牌传播策略;②优化媒体发稿、KOL合作与短视频运营的资源配置与ROI;③借助AI平台实现传播内容的精准触达、效果监测与风险控制;④提升品牌在技术可信度、用户信任与市场影响力方面的综合竞争力。; 阅读建议:建议结合传声港平台的实际工具模块(如AI选媒、达人匹配、数据驾驶舱)进行对照阅读,重点关注各阶段的标准化流程与数据指标基准,将理论策略与平台实操深度融合,推动品牌传播从经验驱动转向数据与工具双驱动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值