码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 能带你起飞的【数据结构】成王第九篇:二叉树2


    目录

    前言

    二叉树的基本基本操作

    获取树中节点的个数

    获取叶子节点的个数 

     获取第K层节点的个数

    获取二叉树的高度 

    检测值为val的元素是否存在 

    判断一棵树是不是完全二叉树 


    前言

    二叉树的基本基本操作

    获取树中节点的个数

    遍历思路代码:

    1. //获取树中节点的个数
    2. int count = 0;
    3. public int size1(BTNode root){
    4. if(root == null) {
    5. return 0;
    6. }
    7. count ++;
    8. count += size1(root.left);
    9. count += size1(root.right);
    10. return count;
    11. }

    子问题思路:

    1. //子问题思路
    2. public int size2(BTNode root){
    3. if (root == null){
    4. return 0;
    5. }
    6. return size2(root.left) + size2(root.right) + 1;
    7. }

    获取叶子节点的个数 

    遍历思路:

    遍历到叶子节点,就让计数器++

    叶子节点:root.left == null && root.right == null

    1. public int leafNodes(BTNode root){
    2. int count = 0;
    3. if(root == null){
    4. return 0;
    5. }
    6. if(root.left == null && root.right == null){
    7. count++;
    8. }
    9. leafNodes(root.left);
    10. leafNodes(root.right);
    11. return count;
    12. }

    子问题思路:

    左树叶子节点加右树叶子节点

    1. public int leafNodes(BTNode root){
    2. if(root == null){
    3. return 0;
    4. }
    5. if(root.left == null && root.right == null){
    6. return 1;
    7. }
    8. return leafNodes(root.left) + leafNodes(root.right);
    9. }

     获取第K层节点的个数

    假设K等于3

    也就是A的第三层

    B,C的第二层

    D,E,F,G的第一层

    终止条件K等于1

    1. public int getKLeveLNodeCount(BTNode root, int k){
    2. if(root == null || k <= 0){
    3. return 0;
    4. }
    5. if(k == 1){
    6. return 1;
    7. }
    8. return getKLeveLNodeCount(root.left,k - 1) + getKLeveLNodeCount(root.right ,k-1);
    9. }

    获取二叉树的高度 

     

    子问题思路

    左树的高度和右树的高度取最大值,然后加1 = 整棵树的高度

    1. public int getHeight(BTNode root){
    2. if(root == null){
    3. return 0;
    4. }
    5. int leftHeight = getHeight(root.left);
    6. int rightHeight = getHeight(root.right);
    7. return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;

    检测值为val的元素是否存在 

    1. public BTNode find(BTNode root,char val){
    2. if(root == null){
    3. return null;
    4. }
    5. if(root.val == val){
    6. return root;
    7. }
    8. BTNode ret = find(root.left,val);
    9. if(ret != null){
    10. return ret;
    11. }
    12. ret = find(root.right,val);
    13. if(ret != null){
    14. return ret;
    15. }
    16. return null;
    17. }

    判断一棵树是不是完全二叉树 

    1. public boolean getHeight3(BTNode root){
    2. if (root == null) return true;
    3. Queue<BTNode> queue = new LinkedList<>();
    4. queue.offer(root);
    5. while (!queue.isEmpty()){
    6. BTNode cur = queue.poll();
    7. if (cur != null){
    8. queue.offer(cur.left);
    9. queue.offer(cur.right);
    10. }else {
    11. break;
    12. }
    13. }
    14. while (!queue.isEmpty()){
    15. BTNode ret = queue.peek();
    16. if(ret != null){
    17. return false;
    18. }
    19. queue.poll();
    20. }
    21. return true;
    22. }

     

     

  • 相关阅读:
    VMware Ubuntu 共享文件夹
    web蓝桥杯真题:绝美宋词
    【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
    Laya---淘宝小程序
    tomcat优化、nginx +tomcat 部署 (三)
    华硕主板升级更新BIOS版本
    前端开发引入element plus与windi css
    Linux上单机部署RocketMq
    RuntimeError: DataLoader worker (pid(s) 46220) exited unexpectedly
    idea Springboot 校园助学贷款系统VS开发mysql数据库web结构java编程计算机网页源码maven项目
  • 原文地址:https://blog.csdn.net/m0_64397675/article/details/125424771
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号