BST二叉搜索树的建树和先序遍历

http://acm.hdu.edu.cn/showproblem.php?pid=3999

 

Problem Description
As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
1.  insert a key k to a empty tree, then the tree become a tree with
only one node;
2.  insert a key k to a nonempty tree, if k is less than the root ,insert
it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.
 


 

Input
There are multiple test cases in an input file. The first line of each testcase is an integer n(n <= 100,000),represent the number of nodes.The second line has n intergers,k1 to kn,represent the order of a tree.To make if more simple, k1 to kn is a sequence of 1 to n.
 


 

Output
One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.
 


 

Sample Input
  
  
4 1 3 4 2
 


 

Sample Output
  
  
1 3 2 4
 

 

简单的二叉搜索树,考察建树和遍历的知识。这里用了递归建树,因为代码比较简短。

附上代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 100005
int a[maxn];
typedef struct  Node
{
    int key;
    Node *left,*right;
}node;
node* create(node *t,int key)   //递归建树
{
    if(!t)
    {
        t=(node *)malloc(sizeof(node));
        t->key=key;
        t->left=NULL;
        t->right=NULL;
    }
    else if(key>t->key)
        t->right=create(t->right,key);
    else if(key<t->key)
        t->left=create(t->left,key);
    return t;
}
int first=1;
void priority(node *t)   //先序遍历
{
    if(t!=NULL)
    {
		if(first)
		{
			first=0;
			printf("%d",t->key);
		}
		else
			printf(" %d",t->key);
        priority(t->left);
        priority(t->right);
    }

}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
		int i;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        node *root;
        root=(node *)malloc(sizeof(node));
		root->key=a[0];
		root->left=NULL;
		root->right=NULL;
        for(i=1;i<n;i++)
            create(root,a[i]);
        priority(root);
        printf("\n");
    }
    return 0;
}


 

 

题目

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搜索(Binary Search Tree,简称BST)是一种特殊的二,它的每个节点的值都大于其左子中的所有节点的值,且小于其右子中的所有节点的值。根据BST的特性,我们可以通过比较节点的值来确定最近公共祖。 首,我们需要根据给定的列构建二搜索。构建BST的方法是从根节点开始,依次将列中的元素插入到中。如果插入的元素小于当前节点的值,则将其插入到左子中;如果插入的元素大于当前节点的值,则将其插入到右子中。 接下来,我们需要找出任意两个节点的最近公共祖。最近公共祖是指在二中同时是这两个节点的祖且深度最大的节点。为了找到最近公共祖,我们可以从根节点开始遍搜索,根据节点的值与给定两个节点的值的大小关系,不断更新当前节点为其左子或右子中的节点,直到找到最近公共祖为止。 以下是一个示例代码,演示了如何构建二搜索并找到任意两个节点的最近公共祖: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildBST(preorder): if not preorder: return None root = TreeNode(preorder[0]) i = 1 while i < len(preorder) and preorder[i] < root.val: i += 1 root.left = buildBST(preorder[1:i]) root.right = buildBST(preorder[i:]) return root def findLCA(root, p, q): if not root: return None if p.val < root.val and q.val < root.val: return findLCA(root.left, p, q) elif p.val > root.val and q.val > root.val: return findLCA(root.right, p, q) else: return root # 示例输入 preorder = [6, 2, 0, 4, 3, 5, 8, 7, 9] p = TreeNode(2) q = TreeNode(8) # 构建二搜索 root = buildBST(preorder) # 找到最近公共祖 lca = findLCA(root, p, q) print("最近公共祖的值为:", lca.val) ``` 输出结果为: ``` 最近公共祖的值为: 6 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值