# UVA-548Tree（二叉树的递归遍历）

Tree

 Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Description

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

## Input

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

## Output

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

## Sample Input

```3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
```

## Sample Output

```1
3
255
```

```#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define SI(x) scanf("%d",&x)
#define mem(x,y) memset(x,y,sizeof(x))
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXN=100010;
int v1[MAXN],v2[MAXN];
char s[MAXN];
int ans,minlength;
struct Node{
int v;
Node *L,*R;
Node(){
L=NULL;R=NULL;
}
};
int init(char *s,int *v){
int k=0;
for(int i=0;s[i];i++){
while(isdigit(s[i]))v[k]=v[k]*10+s[i++]-‘0‘;
k++;
}
//	for(int i=0;i<k;i++)printf("%d ",v[i]);puts("");
return k;
}
int find(int n,int *v,int t){
for(int i=n-1;i>=0;i--)
if(v[i]==t)return i;
return 0;
}
Node* build(int n,int *v1,int *v2){
Node *root;
if(n<=0)return NULL;
root=new Node;
root->v=v2[n-1];
int p=find(n,v1,v2[n-1]);
root->L=build(p,v1,v2);
root->R=build(n-p-1,v1+p+1,v2+p);
return root;
}
void dfs(Node *root,int length){
if(root==NULL)return;
length+=root->v;
if(root->L==NULL&&root->R==NULL){
if(minlength>length)
{
minlength=length;
ans=root->v;
}
else if(minlength==length)
ans=min(ans,root->v);
return;
}
dfs(root->L,length);
dfs(root->R,length);
}
int main(){
while(gets(s)){
mem(v1,0);mem(v2,0);
init(s,v1);
gets(s);
int k=init(s,v2);
Node *root=build(k,v1,v2);
ans=minlength=INF;
dfs(root,0);
printf("%d\n",ans);
}
return 0;
}
```

## 二叉树的递归遍历

#include<iostream> #include<stack> using namespace std; /*二叉树的前序遍历,按照 根节点->左孩子->右孩子 */ typedef struct node { char data; struct node *lchild,*rchild; }BinTree; void creatBinTree(BinTree * &root){ char ch; if(ch=getchar()){ if(ch=='#')

## 二叉树的递归遍历和非递归遍历（附详细例子）

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

## 二叉树的递归遍历和非递归遍历的实现

struct BinaryTreeNode{    int m_nValue;    BinaryTreeNode *m_pLeft;    BinaryTreeNode *m_pRight;}; //递归实现二叉树的遍历.递归算法比较简洁易懂这一就不做解释 void Preorder(BinaryTreeNode *pRoot){    if (pRoot == NULL)    {        return;    }    cout<<pRoot->m_nValue<<