PAT1043 Is It a Binary Search Tree

这个题目是考察二查搜索树,但其实实际上并不需要我们建立一个二叉树,我们只需要在重构的过程中,利用递归的思想直接进行一次遍历即可。

本代码中使用到了lambda表达式,所以代码量比较简洁,只有40行,c++里面还真有很多有趣的特性。

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

int N;
int A[1005];
vector<int> res;//存放输出结果
int value;//临时变量,用于lambda函数内部
function<int (int)> func = [=](int i){return i>=value;}; //lambda函数指针
void tree(int* a, int* b){
	if(a>=b)
		return; //递归出口
	value = *a;
	int* center = find_if(a+1,b,func);  //数组分成3部分,分别是:根元素 左子树 右子树
	if(!all_of(center,b,func)) //测试右子树是否满足条件
		return;
	tree(a+1,center);    //遍历左子树
	tree(center,b);      //遍历右子树
	res.push_back(*a);   //存储输出
}
int main(){
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d",A+i);
	}
	if(N>1 && A[0]<=A[1])
		func = ([=](int i){return i<value;});//如果是mirror,改变lambda函数
	tree(A,A+N);
	if(res.size()!=N)
		printf("NO\n");
	else{//输出
		printf("YES\n");
		printf("%d",res[0]);
		for(int i=1;i<res.size();i++)
			printf(" %d",res[i]);
	}
	return 0;
}
时间: 2024-10-12 21:37:27

PAT1043 Is It a Binary Search Tree的相关文章

pat1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

[LeetCode]Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Binary Search Tree Iterator

QUESTION Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o