暴力算法:二叉树

1.小球下落
思路:直接模拟最后一个小球的下落路线。
具体实现:
第0层:l = l , I是奇数,它是往左走的第( I+1 ) / 2个小球;I是偶数,它是往右走的第 I / 2个小球。第1层:I = ( I+1 ) / 2或 l / 2 ……

#include<cstring>
#include<cstdio>
using namespace std ;
int main() {
 int D , I ;
 while ( scanf("%d%d",&D,&I) == 2 ) {
  int k = 1 ;
  for ( int i = 0 ; i < D - 1 ; i ++ ) {
   if ( I % 2 ) {
    k = k * 2 ;
    I = ( I + 1 ) / 2 ;
   }
   else {
    k = k * 2 + 1 ;
    I = I / 2 ;
   }
  }
  printf("%d",k) ;
 }
} 

2.树的层次遍历
1).读取数据:每读入一个就处理:
读到结束符号:if( !strcmp( s , "()" ) ) break; ( strcmp字符串比较函数,若相同返回0);
取节点值:sscanf( &s[1] , "%d" , &v );
取路径:strchr( s , ',' ) + 1( strchr( s , ‘,’ )返回s里第一个字符“,”的指针);
2).建树:结点用指针的形式储存,每个结点是一个指向结构体的指针。此结构体包括有无赋值、左子节点、右子节点、结点值。每次读入一个结点,读入节点后,创建路径上的所有结点;
3).二叉树的BFS遍历:创建一个集合vector(用于输出,用前先clear),一个队列queue(用于遍历过程)。Queue用于保存每个要找左右子节点的父节点,找到的子节点入queue,父节点出queue入vector;
代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std ;

char s[maxn] ;
bool read_input() {
	failed = false ;
	root = newnode() ;
	for( ; ; ) {
		if( scanf("%s",s) != 1 ) return false ;
		if( !strcmp( s , "()" ) ) break ;
		int v ;
		sscanf( &s[1] , "%d" , &v ) ;
		addnote( v , strchr( s , ',' ) + 1 ) ;
	}
	return true ;
}

struct Node{
	bool have_value ;
	int v ;
	Node() : left( NULL ) , right( NULL ) , have_value( false ) {} 
};

Node *root ;
Node *newnode() {
	return new Node() ;
}

void addnote( int v , char *s ) {
	Node *u = root ;
	int lon = strlen(s) ;
	for( int i = 0 ; i < lon ; i ++ ) {
		if( s[i] == 'L' ) {
			if( u->left == NULL ) u->left = newnode() ;
			u = u->left
		}
		if( s[i] == 'R' ) {
			if( u->right == NULL ) u->right = newnode() ;
			u = u->right ;
		}
		if( u->have_value ) failed = true ;
		u->v = v ;
		u->have_value = true ;
	}
}

bool bfs( vector<int> &ans ) {
	queue<Node*> q ;
	ans.clear() ;
	q.push(root) ;
	while( !q.empty() ) {
		Node *u = q.front() ;
		q.pop() ;
		if( !u->have_value ) return false ;
		if( u->left != NULL ) q.push( u->left ) ;
		if( u->right != NULL ) q.push( u->right ) ;
		ans.push_back( u->v ) ;
	}
	return true ;
}

3.树
1).读取数据:每次输入一行,把字符串转成数组。
输入字符串:
string line ; if( !getline( cin , line ) ) return false ;
将字符串转换成一个个数字:(且统计出数组长度)

streamstring ss( line ) ;
int x ;
n = 0 ;
while( ss >> x ) a[n++] = x ;

2).构造二叉树:得到数组lch[结点个数+1]和rch[结点个数+1],结点编号就是节点值,数组内容是此节点的左子节点(lch)和右子节点(rch),若没有子结点就均为0;
比如lch[4]=3,rch[4]=2: 在这里插入图片描述
3).构造dfs函数,通过lch[]=rch[]=0得到叶节点,如何保证遍历每一个结点:遍历开始处:根结点,第二个是lch[root]和rch[root];
代码:

#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std ;

const int maxn = 10000 + 10 ;
int in_order[maxn] , post_order[maxn] , lch[maxn] , rch[maxn] ;
int n ;
bool read_list( int *a ) {
	string line ;
	if( !getline( cin , line ) ) return false ;
	streamstring ss( line ) ;
	int x ;
	n = 0 ;
	while( ss >> x ) a[n++] = x ;	
}

int solve( int L1 , int R1 , int L2 , int R2 ) {
	if( L1 > R2 ) return 0 ;
	int root = post_order[R2]
	int p = L1 ;
	while( in_oeder[p] != root ) p ++ ;
	int cnt = p - L1 ;
	lch[root] = solve( L1 , p - 1 , L2 , L2 + cnt - 1 ) ;
	rch[root] = solve( p + 1 , R1 , L2 + cnt , R2 - 1 ) ;
	return root ;
}

int best , best_sum ;
void dfs( int u , int sum ) {
	int sum += u ;
	if( !lch[u] && !rch[u] ) {
		if( sum < best_sum || ( sum == best_sum && u < best ) ) {
			best_sum = sum ;
			best = u ;
		}
	}
	if( ich[u] ) dfs( lch[u] , sum ) ;
	if( rch[u] ) dfs( rch[u] , sum ) ;
}

int main() {
	while( read_list( in_order ) ) {
		read_list( post_order ) ;
		int n = strlen( in_order ) ;
		solve( 0 , n - 1 , 0 , n - 1 ) ;
		best_sum = 1000000000 ;
		dfs( post_order[n-1] , 0 ) ;
		printf("%d\n",best) ;
	}
	return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值