我的DFS

void DFS(Node* root){if(root==NULL)return;if(root->lchild){root->lchild->layer = root->layer+1;cnt[root->lchild->layer] ++;maxLayer = max(maxLayer,root->lchild->layer);DFS(root->lchild);}if(root->rchild){root->rchild->layer = root->layer+1;cnt[root->rchild->layer] ++;maxLayer = max(maxLayer,root->rchild->layer);DFS(root->rchild);}
}

柳婼仙女的DFS

void DFS(Node* root,int depth){if(root==NULL){maxLayer = max(maxLayer,depth);return;}cnt[depth] ++;DFS(root->lchild,depth+1);DFS(root->rchild,depth+1);
}

注意:对左右子结点进行DFS之前不可以判空,否则maxLayer永不会更新!

AC代码

#include<cstdio>
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>using namespace std;const int maxn = 1001;int n;
int cnt[maxn] = {0};
int maxLayer = -1;struct Node{int v;Node *lchild,*rchild;
};void insert(Node* &root,int x){if(root==NULL){root = new Node;root->v = x;root->lchild = root->rchild = NULL;return;}if(x<=root->v)insert(root->lchild,x);if(x>root->v)insert(root->rchild,x);
}void DFS(Node* root,int depth){if(root==NULL){maxLayer = max(maxLayer,depth);return;}cnt[depth] ++;DFS(root->lchild,depth+1);DFS(root->rchild,depth+1);
}int main(){cin>>n;Node* root = NULL;for(int i=0;i<n;i++){int x;cin>>x;insert(root,x);}DFS(root,0);printf("%d + %d = %d\n",cnt[maxLayer-1],cnt[maxLayer-2],cnt[maxLayer-1]+cnt[maxLayer-2]);return 0;
}

1115 Counting Nodes in a BST相关推荐

  1. PAT甲级1115 Counting Nodes in a BST (30分):[C++题解] 递归建二叉搜索树、dfs求一层结点数量

    文章目录 题目分析 题目链接 题目分析 分析 首先本题给定的二叉搜索树的定义和其他地方的不同.本题小于等于的是左子树,右子树是大于根结点的. 然后说一下做题的思路. 给定一串数据,让构造二叉搜索树. ...

  2. 1115 Counting Nodes in a BST(甲级)

    1115 Counting Nodes in a BST (30分) A Binary Search Tree (BST) is recursively defined as a binary tre ...

  3. 1115. Counting Nodes in a BST (30) 数据结构

    给出一颗BST树 求这个数最后两层的节点数量 用c1+c2 = n 的形式给出 建树深度搜索即可 #include<iostream> #include<cstdio> #in ...

  4. 1115 Counting Nodes in a BST (30 分)【难度: 一般 / 知识点: 构建二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 很经典的构建搜索二叉树. #include& ...

  5. Two nodes of a BST are swapped, correct the BST

    Two nodes of a BST are swapped, correct the BST[转载] Two of the nodes of a Binary Search Tree (BST) a ...

  6. Two nodes of a BST are swapped, correct the BST(恢复两个节点被交换的BST)

    Two nodes of a BST are swapped, correct the BST(恢复两个节点被交换的BST) Q: BST的两个节点被交换了,修复它: Input Tree:10/ \ ...

  7. oracle 连接组件,[2021] node连接oracle数据库示例[使用oracle官方组件]

    [2021] node连接oracle数据库示例[使用oracle官方组件] node 连接 oracle 示例 本示例采用的 oracledb 和 instantclient-basic-windo ...

  8. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

  9. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

最新文章

  1. LightRoom操作快捷键
  2. linux 管理命令 之 管理时间
  3. 前端工程师应该达到什么水平,找工作薪资才比较高?
  4. Java与MySQL连接报错_java连接数据库报错(
  5. 过磅系统_集团公司首个过磅计量及销售结算新系统上线试运行
  6. 俄罗斯电力公司T Plus完成25MW光伏电站
  7. ibm服务器单盘从装系统,thinkpad系统重装图文详解
  8. 企业资源计划(ERP)原理与实践第二章
  9. 做好ASO优化方案,步骤。
  10. python为什么卸载不了_为什么Python3.8删除不了?
  11. Vant2 源码分析之 vant-sticky
  12. 表格拖拽、树状拖拽的各种方法
  13. 用php打出2020年是庚子鼠年,2020是庚子金鼠年 说说鼠的那些事
  14. 浅谈 React Fiber
  15. EPS创建三维模型-(osgb数据在EPS中的转换数据)
  16. 计算机游戏的作文,第一次玩电脑游戏作文
  17. 欢迎使用Mythtype6.9b编辑器
  18. ios6应用运行在ios7系统上
  19. 吴恩达机器学习 学习笔记 之 一 监督学习和无监督学习
  20. 摇一摇手机控制手机录音功能

热门文章

  1. mpvue 转uni-app 操作记录
  2. JS删除数组指定下标并添加到数组开头
  3. vue打包后图片找不到情况
  4. 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】搭建HAWQ数据仓库01 —— 准备环境,搭建本地仓库,安装ambari...
  5. Aria2打造属于自己的下载神器
  6. 【快速入门系列】简述 for...in 和 for...of 区别
  7. nio selector
  8. AAC_LC用LATM封装header信息解析 Audio Specific Config格式分析
  9. java List集合中contains方法总是返回false
  10. 使用SQL Server维护计划实现数据库定时自动备份