pat 1115. Counting Nodes in a BST (30)
来源:互联网 发布:java中prototype 编辑:程序博客网 时间:2024/05/21 10:34
https://www.patest.cn/contests/pat-a-practise/1115
#include <cstdio>typedef struct node {int v;struct node * left, * right;node(int x) :left(NULL),right(NULL){v = x;}}Node;int deep,n1,n2;void insertBST(Node *&root, int x,int l) {if (root == NULL) {root = new node(x);if (l > deep) deep = l;return ;}if (x <= root->v) {insertBST(root->left, x, l + 1);}else {insertBST(root->right, x, l + 1);}return ;}void dfs(Node * root,int l) {if (root == NULL) return;if (l == deep) {n1++;}else if (l == deep - 1) {n2++;}dfs(root->left,l+1);dfs(root->right, l + 1);}int main(){int n,x;Node * root = NULL;deep = 0,n1=0,n2=0;scanf("%d",&n);for (int i = 0; i < n; i++){scanf("%d", &x);insertBST(root, x, 0);}dfs(root,0);printf("%d + %d = %d\n", n1, n2, n1 + n2); return 0;}
0 0
- PAT--1115. Counting Nodes in a BST
- 【PAT】1115. Counting Nodes in a BST
- PAT A 1115. Counting Nodes in a BST (30)
- solution Of Pat 1115. Counting Nodes in a BST (30)
- PAT 1115. Counting Nodes in a BST (30)
- pat 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30) PAT 甲级
- 【PAT】【Advanced Level】1115. Counting Nodes in a BST (30)
- PAT 1115. Counting Nodes in a BST (30) 建树
- PAT 甲级 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- Git配置SSH key密钥
- 您好。请多多指教
- Omi教程-组件通讯
- POJ1088 滑雪(记忆化搜索入门级题目)
- 239. Sliding Window Maximum
- pat 1115. Counting Nodes in a BST (30)
- Linux运维第十二讲
- Html常用標簽
- 文章标题
- redis--jedis连接集群
- Day 18 Session核心方法(持久化对象的状态)
- eclipse+环境变量+SDK+虚拟机配置
- MySQL日志系统
- jxl读取excel中Date类型