548 - Tree***

本文介绍了一种利用中序和后序遍历序列构建二叉树的方法,并通过递归方式找到从根节点到叶子节点路径中最短的一条路径。文章详细展示了如何根据给定的数据序列创建二叉树,以及如何遍历该树来确定路径长度最小的叶子节点。
/*
题意:
给出中序和后序数据,创建一颗二叉树。
再根据从根到叶子的路径长,输出路经最短的那个叶子节点


纠错时,自己测试一组数据。
效果+1
*/

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <sstream>
using namespace std;

struct Node
{
	Node *lchild,*rchild;
	int v;
	Node()
	{
		lchild=rchild=NULL;
		v=0;
	}
};
int inorder[10010],postorder[10010];
int result,value,min_value;
string a,b;
int len;

int init()
{
	istringstream a_in(a);
	istringstream b_in(b);
	int temp;
	len=0;
	while(a_in>>temp)
		inorder[len++]=temp;
	len=0;
	while(b_in>>temp)
		postorder[len++]=temp;
	return  len;
}

int getPos(int c,int *t)
{
	for(int i=0;i<len;i++)
		if(c==t[i])
			return i;
	return 0;
}

Node *createTree(int n,int *t1,int *t2)
{
	if(n<=0)
		return NULL;
	Node *root=new Node;
	root->v=t2[n-1];
	int pos=getPos(t2[n-1],t1);
	root->lchild=createTree(pos,t1,t2);
	root->rchild=createTree(n-pos-1,t1+pos+1,t2+pos);
	return root;
}

void traverse(Node *root)
{
	value+=root->v;
	if(root->lchild==NULL && root->rchild==NULL)
	{
		if(value<min_value)
		{
			min_value=value;
			result=root->v;
		}
		//In the case of multiple paths of least value you should pick the one with the least value on the terminal node.
		//原先没有考虑也通过了,数据太弱了。
		else if(value==min_value)
		{
			result=result<root->v?result:root->v;
		}
	}
	if(root->lchild!=NULL)
	{
		traverse(root->lchild);
	}
	if(root->rchild!=NULL)
	{
		traverse(root->rchild);
	}
	value-=root->v;
	//如果不使用回溯,将value设置成形参也可
}

int main()
{	
	//freopen("data.in","r",stdin);
	while(getline(cin,a) && getline(cin,b))
	{
		int len=init();
		Node *root=createTree(len,inorder,postorder);
		min_value=1000000000;
		value=0;
		result=0;
		traverse(root);
		printf("%d\n",result);
	}
	return 0;
}


Startpoint: uC410_uNoRAM_uMain_uLsu_uC400LsuDataCacheController_DRamDataIn_reg_33_ (rising edge-triggered flip-flop clocked by CORE_CLK) Endpoint: uC410_uRAM_uDDataRAM16KB_uDataRAM0o (rising edge-triggered flip-flop clocked by CORE_CLK') Last common pin: uC410_uNoRAM_uMain_U1586/ZN Path Group: CORE_CLK Path Type: max Max Data Paths Derating Factor : 1.000 Min Clock Paths Derating Factor : 0.930 Point Incr Path ---------------------------------------------------------------------------------------------------------------------------- clock CORE_CLK (rise edge) 0.000 0.000 clock network delay (propagated) 2.540 2.540 uC410_uNoRAM_uMain_uLsu_uC400LsuDataCacheController_DRamDataIn_reg_33_/CP (SDFQLVTD0) 0.000 2.540 r uC410_uNoRAM_uMain_uLsu_uC400LsuDataCacheController_DRamDataIn_reg_33_/Q (SDFQLVTD0) 0.177 & 2.716 r FE_OFC2456_uC410_uNoRAM_uMain_uLsu_uC400LsuDataCacheController_n1689/Z (BUFFLVTD3) 0.388 & 3.104 r FE_OFC2457_uC410_uNoRAM_uMain_uLsu_uC400LsuDataCacheController_n1689/Z (BUFFLVTD2) 0.399 & 3.503 r uC410_uRAM_uDDataRAM16KB_uDataRAM0o/D[1] (ARM_512x32_byte) 0.161 & 3.664 r data arrival time 3.664 clock CORE_CLK' (rise edge) 1.250 1.250 clock network delay (propagated) 1.997 3.247 clock reconvergence pessimism 0.108 3.355 clock uncertainty -0.100 3.255 uC410_uRAM_uDDataRAM16KB_uDataRAM0o/CLK (ARM_512x32_byte) 3.255 r library setup time -0.140 3.115 data required time 3.115 ---------------------------------------------------------------------------------------------------------------------------- data required time 3.115 data arrival time -3.664 ---------------------------------------------------------------------------------------------------------------------------- slack (VIOLATED) -0.548
06-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值