1. 程式人生 > >PAT 1115 Counting Nodes in a BST

PAT 1115 Counting Nodes in a BST

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 or equal to the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000) which is the size of the input sequence. Then given in the next line are the N integers in [−10001000] which are supposed to be inserted into an initially empty binary search tree.

Output Specification:


For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:

n1 + n2 = n
where n1 is the number of nodes in the lowest level, n2 is that of the level above, and n is the sum.

Sample Input:

9
25 30 42 16 20 20 35 -5 28

Sample Output:

2 + 4 = 6

解題思路:

struct node{
	int v;
	node *left;
	node *right;
};

其次根據二叉搜尋樹的性質,每次遞迴還原根節點;

node* build(node *root,int v){
	if(root==NULL){
		root=new node();
		root->v=v;
		root->left=NULL;
		root->right=NULL;
	}else if(v>root->v){
		root->right=build(root->right,v);
	}else{
		root->left=build(root->left,v);
	}
	return root;
}

最後基於深度優先遍歷,找到每個節點所屬的層數。

#include <iostream>
#include<cstdio>
using namespace std;
struct node{
	int v;
	node *left;
	node *right;
};
int num[10000];
node* build(node *root,int v){
	if(root==NULL){
		root=new node();
		root->v=v;
		root->left=NULL;
		root->right=NULL;
	}else if(v>root->v){
		root->right=build(root->right,v);
	}else{
		root->left=build(root->left,v);
	}
	return root;
}

int maxdepth=-1;
void dfs(node *root,int depth){
	if(root==NULL){
		if(depth>maxdepth) maxdepth=depth;
		return;
	}
	num[depth]++;
	dfs(root->left,depth+1);
	dfs(root->right,depth+1);
}
int main(int argc, char** argv) {
	int n;
	scanf("%d",&n);
	node *root=NULL;
	for(int i=0;i<n;i++){
		int x;
		scanf("%d",&x);
		root=build(root,x);
	}
	dfs(root,0);
	printf("%d + %d = %d",num[maxdepth-1],num[maxdepth-2],num[maxdepth-1]+num[maxdepth-2]);
	return 0;
}

相關推薦

PAT 1115 Counting Nodes in a BST

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node

PAT1115 Counting Nodes in a BST【dfs】

1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following prope

PAT (Advanced Level) Practice 1115 Counting Nodes in a BST (30 分)

排序二叉樹,注意看定義 #include<cstdio> using namespace std; const int N=1e3+5; int ch[N][2],key[N],rt,dfn,level[N]; void insert(int x) { ke

1115 Counting Nodes in a BST (30 分)

1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The

light oj 1094 Farthest Nodes in a Tree(樹的直徑模板)

fas problems tro tree enter have () 分享 color 1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2

[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST

-o bsp ive http always += cnblogs trac check Given a binary search tree, write a function kthSmallest to find the kth smallest element in

230. Kth Smallest Element in a BST

pro new lee public http ack urn nod light https://leetcode.com/problems/kth-smallest-element-in-a-bst/#/solutions public int kthSmallest

[GeeksForGeeks] Populate inorder successor for all nodes in a binary search tree

stack iter pro get root following sin ice nod Given a binary search tree with the following tree node definition. next points to a node‘s

Leetcode 230: Kth Smallest Element in a BST

pre pop valid operation order not may nodes tree Given a binary search tree, write a function kthSmallest to find the kth smallest elemen

450. Delete Node in a BST

com IT date into asi for style 葉子 divide Given a root node reference of a BST and a key, delete the node with the given key in the BST. R

LeetCode-230 kth smallest element in a bst 二叉搜尋樹中第K小的元素

題目連結 https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 題意 中文題,對於二叉搜尋樹而言,找其中的第K小的數 題解         很有趣的題,但是很簡單

[LeetCode] 450. Delete Node in a BST

Delete Node in a BST Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node referen

Delete Node in a BST 二叉查詢樹的查詢、插入和刪除 - Java實現

https://leetcode.com/problems/delete-node-in-a-bst Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return t

450. Delete Node in a BST(python+cpp)

題目: Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibl

Google面試題專題6 - leetcode230. Kth Smallest Element in a BST/139. Word Break

230. Kth Smallest Element in a BST 題目描述 給定一顆二叉搜尋樹,編寫函式kthSmallest找到第k小元素。(1 ≤ k ≤ BST的總元素個數) 例子 Example 1: Input: root = [3,1,4,nul

python leetcode 450. Delete Node in a BST

用遞迴做 程式碼看起來簡潔點 以下是用執行時間的增加換取了程式碼的整潔 如果key的右節點存在那麼key的值和右節點的最最最最…左節點的值交換 繼續遞迴直到再一次找到key值 也就是說會操作兩次 但還是能滿足了O(h)的執行時間 class Solution: def delet

[LeetCode] 230. Kth Smallest Element in a BST

題目 Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is alwa

#Leetcode# 230. Kth Smallest Element in a BST

https://leetcode.com/problems/kth-smallest-element-in-a-bst/   Given a binary search tree, write a function kthSmallest to find the k

【LeetCode】 230. Kth Smallest Element in a BST(非遞迴中序遍歷二叉樹)

因為這是一棵二叉搜尋樹,所以找它的第 kkk 小值,就是這棵二叉搜尋樹的中序遍歷之後的第 kkk 個數,所以只需要將這棵樹進行中序遍歷即可,下面程式碼是非遞迴形式的二叉樹中序遍歷。 程式碼如下: /**

EularProject 73:Counting fractions in a range

Andrew Zhang Nov 11, 2017 Consider the fraction, n/d, where n and d are positive integers. If n < d and HCF(n,d)=1, it is called a reduced