1110 Complete Binary Tree

在这里插入图片描述在这里插入图片描述

题目大意:

判断是不是完全二叉树,是则输出最后一个节点的下标,否则输出根节点下标。

解题思路:

一开始从输入上投机(只有右子没有左子肯定不是,只有左子没有右子但是数量超过2也不是),只拿到18分(忽略了右子树饱满没有左子树的情况)。正确的做法是维护一个下标参数(指完全二叉树结点与位置对应关系),DFS遍历树,如果最大结点的编号正好等于最大的下标证明是一个完全二叉树,否则必然有空缺的位置(该下标下没有元素)。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
struct node
{
	int lchild=-1,rchild=-1;
}nod[30];
int getnum(string s)//字符串转整数 
{
	int sum=0;
	for(int i=0;i<s.length();i++)
	{
		sum=sum*10+(s[i]-'0');
	}
	return sum;
}
int ma=-1,tail;
void dfs(int root,int inde)
{
	if(inde>ma)//更新最大的下标 
	{
		ma=inde;
		tail=root;
	}
	if(nod[root].lchild!=-1)dfs(nod[root].lchild,inde*2);//左儿子下标理论上就是2*inde 
	if(nod[root].rchild!=-1)dfs(nod[root].rchild,inde*2+1);
}
int main()
{
	int book[30]={0};
	cin>>n;
	for(int i=1;i<=n;i++)//我是从1开始的,方便算下标 
	{
		string l,r;
		cin>>l>>r;
		if(l!="-")
		{
			int num=getnum(l)+1;
			nod[i].lchild=num;
			book[num]=1;
		}
		if(r!="-")
		{
			int num=getnum(r)+1;
			nod[i].rchild=num;
			book[num]=1;
		}
	 } 
	 int root;
	 for(int i=1;i<=n;i++)//找根节点 
	 {
	 	if(!book[i])
	 	{
	 		root=i;
	 		break;
		 }
	 }
	 dfs(root,1); 
	 if(ma==n)cout<<"YES "<<tail-1<<endl;
	 else cout<<"NO "<<root-1<<endl;
	 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To convert the given array to a complete BST, we need to perform the following steps: 1. Sort the array in ascending order 2. Construct a complete binary tree using the sorted array 3. Perform inorder traversal of the binary tree and store the elements in the original array in the same order as the traversal Here's the implementation of the to_bst(lst) function in Python: ```python def to_bst(lst): # Sort the input list lst.sort() # Construct a complete binary tree using the sorted list n = len(lst) if n == 0: return lst root = lst[n // 2] left_subtree = to_bst(lst[:n // 2]) right_subtree = to_bst(lst[n // 2 + 1:]) binary_tree = [root] + left_subtree + right_subtree # Perform inorder traversal of the binary tree and store the elements in the original array inorder_traversal(binary_tree, lst, 0) return lst def inorder_traversal(binary_tree, lst, i): # Perform inorder traversal of the binary tree and store the elements in the original array n = len(binary_tree) if i >= n: return inorder_traversal(binary_tree, lst, 2 * i + 1) lst[i] = binary_tree[i] inorder_traversal(binary_tree, lst, 2 * i + 2) ``` The to_bst(lst) function takes in the input list and returns the same list after converting it to a complete BST. The function first sorts the input list in ascending order. It then constructs a complete binary tree using the sorted list by recursively dividing the list into two halves and setting the middle element as the root of the binary tree. Finally, the function performs an inorder traversal of the binary tree and stores the elements in the original list in the same order as the traversal.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值