【TOJ 1002】

describe

Given a binary search tree (Binary Search Tree, BST), and a pair of integers x and y, prune the nodes in the binary tree that are not in the interval [x, y], and find the root node value of the pruned BST tree.

BST definition:

A binary search tree, also known as a binary sorting tree, is either an empty tree or a binary tree with the following properties: If its left subtree is not empty, then the values ​​of all nodes in the left subtree are less than The value of its root node; if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node; its left and right subtrees are also binary sorted respectively Tree.

A binary tree node is defined as follows:

struct TreeNode {

    int val;

    struct TreeNode *left;

    struct TreeNode *right;

};

The code of the title part has been completed, you only need to add and submit the following functions:

struct TreeNode* trimBST(struct TreeNode* root, int L, int R);

enter

The first line of input is an integer n, which represents the number of keywords (n<500).

There are n integers in the second line, which represent the keywords of the nodes, and a BST is constructed according to the sequence of the keywords.

The third row is x and y (x<=y).

output

Output the value of the root node after pruning. The data guarantees that there must be a root node after pruning.

sample input

3
1 3 2
2 3

Sample output

3

answer

Prune a binary tree: Given the maximum and minimum bounds L and R of a binary tree, make the elements in the tree lie between L and R.

1. When the root is between L and R, recursively prune its left and right subtrees and return to the root.

2. When the value of root is less than L, its left subtree is smaller than L, so discard its left subtree, prune its right subtree recursively, and return the pruned right subtree.

3. When the value of root is greater than R, its right subtree is greater than R, so discard its right subtree, prune its left subtree recursively, and return the pruned left subtree.

#include<bits/stdc++.h>
using namespace std;
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};
void Insert(TreeNode**root,TreeNode*t)
{
    if(*root==NULL)
        *root=t;
    else if(t->val < (*root)->val)
        Insert(&(*root)->left,t);
    else Insert(&(*root)->right,t);
}
TreeNode*creatBst()
{
    TreeNode*root=NULL,*t;
    int x,i,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x);
        t=(TreeNode*)malloc(sizeof(TreeNode));
        t->val=x;
        t->left=NULL;
        t->right=NULL;
        Insert(&root,t);
    }
    return root;
}
TreeNode* trimBST(struct TreeNode* root, int L, int R) { if(!root) return 0; if(root->val<L) return trimBST(root->right,L,R); else if(root->val>R) return trimBST(root->left,L,R); else { root->left=trimBST(root->left,L,R); root->right=trimBST(root->right,L,R); return root; } } intmain () { TreeNode*t=creatBst(); int x,y; cin>>x>>y; printf("%d\n",trimBST(t,x,y)->val); return 0; }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689744&siteId=291194637