1115. Counting Nodes in a BST (30) 二叉树
来源:互联网 发布:java工程发布到tomcat 编辑:程序博客网 时间:2024/05/17 09:11
题目地址
创建二叉搜索树并输出其最低两层的节点数目,使用先序遍历,注意当树只有一层时的情况。
//// Created by aleafall on 16-10-3.//#include<iostream>#include<map>using namespace std;struct Node { int data; Node *lchild, *rchild;} *root;map<int, int> mp;void preOrder(Node *root, int depth) { if (root == NULL) return; if (mp.find(depth) != mp.end()) mp[depth]++; else mp[depth] = 1; preOrder(root->lchild, depth + 1); preOrder(root->rchild, depth + 1);}void insert(Node *&root, int data) { if (root == NULL) { root = new Node; root->data = data; root->lchild = root->rchild = NULL; return; } if (data <= root->data) insert(root->lchild, data); else insert(root->rchild, data);}int main() { int n, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; insert(root, x); } preOrder(root, 1); int n1, n2; if (mp.size() > 1) { map<int, int>::iterator it = --mp.end(); n1 = it->second; n2 = (--it)->second; } else { n1 = 1; n2 = 0; } cout << n1 << " + " << n2 << " = " << n1 + n2 << endl; return 0;}
0 0
- 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)
- 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)(建立二叉搜索树)
- pat甲1115. Counting Nodes in a BST(BST二叉搜索树)
- 1115. Counting Nodes in a BST (30)[bst+dfs遍历]
- PAT A 1115. Counting Nodes in a BST (30)
- PAT--1115. Counting Nodes in a BST
- Object
- CSS Display(显示) 与 Visibility(可见性)
- 2016华为校招上机试题1——交叉排序
- 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
- 判断linux是64位还是32位
- 1115. Counting Nodes in a BST (30) 二叉树
- Java那点事儿总类
- 商城之首页商品分类
- Spring的AOP原理
- 大牛文章:理解矩阵
- Linux配置程序开机自启动
- linux常用命令
- 1116. Come on! Let's C (20) 简单模拟
- Java for Web学习笔记(二九):JSTL(5)FMT Tag(上)