Binary Tree Series title

Binary Tree Series title

1. Solving binary tree properties.

UVA 679 - Dropping Balls

There are a binary tree, the maximum depth is D, and the depth of all the leaves are the same. Left to right, top to bottom, all nodes are numbered 1, 2, 3, ..., 2D-1. Put a small ball at node 1, it will be to fall. Each interior node has a switch, all closed initially, every time when the ball fell on a switch, the status will change. When the ball reaches a node, if the node switch is off, go to the left, right or down, until the leaf nodes come, shown in Figure 6-2.
Here Insert Picture Description
Some turn the ball begins to fall from junction 1 at last where the ball will fall into it? Enter the number leaf leaves little depth D and the number of balls I, I, balls final output is located. Suppose I does not exceed the number of the whole tree foliage. D≤20. Up to 1000 sets of data input.

Method 1: Analog but TLE, the I simulate ball drop, to fall on a switch node will transform larger data 20 * 2 ^ 20.
Method 2: Using recursive parity + Thought the former. two were analyzed: the first left, the second apparent right analysis is available if I go to the right of the last ball is an even number of I / 2 months, otherwise go to the left of (I +. 1) / 2.
Then the current node as the root. Continues to determine a left or right.

TLE simulation codes

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int vis[N];
int main(){
	int t;
	cin>>t;
	while(t--){
		memset(vis,0,sizeof vis);
		int d,n,k=1;
		cin>>d>>n;
		while(n--){
			k=1;
			for(int i=1;i<d;i++)
			{
				vis[k]=!vis[k]; //当球到某个点 该点开关改变.如果此时是开说明之前是关则往左走,反之往右走. 
				k = vis[k]?2*k:2*k+1;
			}
		}
		cout<<k<<endl;
	}
	return 0;
}

AC Code

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int d,n,k=1;
		cin>>d>>n;
		d--;
		while(d--){
			if(n&1) k*=2,n=(n+1)/2;
			else k=k*2+1,n>>=1; 
		}
		cout<<k<<endl;
	}
	return 0;
} 

Summary: This title is a complete binary tree use the root (numbered from 1) of the left son 2n, 2n + 1 is the right son of this nature. Plus parity law analysis results significantly reduce the time. Also numbered from 0 complete binary tree left son 2n + 1, the right son 2n +2

Binary tree traversal.

Here are mainly BFS and DFS. Let's talk about the main DFS.

DFS: divided into three types: the first sequence, after, in order traversal. Sequence preorder: root around . The sequence after the sequence: about root . In order sequence: left and right root
-related properties: the known first order and the order may be determined a tree. Known subsequent sequence and may also determine a tree. It is known that the first sequence and the sequence may not be able to determine a tree. On the following few questions.

Questions 1: string to two, respectively, in the order and the first order, or sequence, respectively, and the subsequent. Let you print the order (first order).
The main idea: the first known sequence or the order may determine the root location. Then find the root of the position in the sequence. On the left is the root of the left subtree, the right for the right child.
And then we were on the left and right subtrees recursively search. That can get results. Here the code.

In the known sequence and the first order, the sequence seek

#include<bits/stdc++.h>
using namespace std;
void dfs(string a,string b){//a:中序字符串 b:先序字符串. 
	if(a.size()>0)
	{
		char c=b[0];//根 
		int k=a.find(c);//根在中序的位置. 
		dfs(a.substr(0,k),b.substr(1,k)); //递归搜索左子树 
		dfs(a.substr(k+1),b.substr(k+1,a.size()-k-1));//递归搜索右子树 
		cout<<c; //最后打印根.  满足左右根的顺序 即为后序遍历. 
	}
}
int main(){  //求后序. 
	string a,b;
	cin>>a>>b;
	dfs(a,b);
	return 0;
} 

And the subsequent sequence is known, find the first-order

#include<iostream>
#include<string>
using namespace std;
void dfs(string a,string b){//a:中序字符串 b:后序字符串 
	 if(a.size()>0) //当子树存在时. 
	 {
	 	char c=b[b.size()-1]; //根为后序的最后位置 
		cout<<c;//先输出根 
		int pos=a.find(c);//根在中序的位置  pos也是左子树的结点数. 
		dfs(a.substr(0,pos),b.substr(0,pos));//dfs 左子树 
		dfs(a.substr(pos+1),b.substr(pos,a.size()-pos-1));	// dfs 右子树 
	 }  //满足根左右的顺序. 
}
int main(){
	string a,b;
	cin>>a>>b;
	dfs(a,b);
	return 0;
}
Published 18 original articles · won praise 14 · views 360

Guess you like

Origin blog.csdn.net/weixin_45750972/article/details/104968107