当前位置: 首页 > article >正文

代码随想录算法训练营Day13

110.平衡二叉树

力扣题目链接:. - 力扣(LeetCode)

后序迭代

class Solution {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root)!=-1;
    }
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftheight=getHeight(root.left);
        int rightheith=getHeight(root.right);
        if(leftheight==-1||rightheith==-1){
            return -1;
        }
        else if(Math.abs(leftheight-rightheith)>1){
          return -1;  
        }
        return Math.max(leftheight,rightheith)+1;
    }
}

257. 二叉树的所有路径

力扣题目链接:. - 力扣(LeetCode)

前序递归

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
            List<String> res=new ArrayList<>();
            if(root==null){
                return res;
            }
            List<Integer> pathnode=new ArrayList<>();
            bianli(root,res,pathnode);
            return res;
    }
    public void bianli(TreeNode root,List<String> res,List<Integer> pathnode){
        pathnode.add(root.val);
        if(root.left==null&&root.right==null){
            StringBuilder str=new StringBuilder();
            for(int i=0;i<pathnode.size()-1;i++){
                str.append(pathnode.get(i)).append("->");
            }
            str.append(pathnode.get(pathnode.size()-1));
            res.add(str.toString());
            return;
        }
        if(root.left!=null){
            bianli(root.left,res,pathnode);
            if(!pathnode.isEmpty()){
                pathnode.remove(pathnode.size()-1);
            }       
        }
        if(root.right!=null){
            bianli(root.right,res,pathnode);
            if(!pathnode.isEmpty()){
                pathnode.remove(pathnode.size()-1);
            }     
        }
        return;
    }
}

404.左叶子之和

力扣题目链接:. - 力扣(LeetCode)

后序递归

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root==null){
            return 0;
        }
        int rightsum=sumOfLeftLeaves(root.right);
        int leftsum=sumOfLeftLeaves(root.left);
        int midsum=0;
        if(root.left!=null&&root.left.left==null&&root.left.right==null){
            midsum=root.left.val;
        }
       int sum=midsum+rightsum+leftsum;
        return sum;
    }
}

222.完全二叉树的节点个数

力扣题目链接:. - 力扣(LeetCode)

层序遍历

class Solution {
    public int countNodes(TreeNode root) {
        if(root==null){
            return 0;
        }
        Deque<TreeNode> myque=new LinkedList<>();
        myque.offer(root);
        int sum=0;
        int res=0;
        while(!myque.isEmpty()){
            sum++;
            int len=myque.size();
            res+=len;
            if(len!=Math.pow(2,sum-1))
            break;
            while(len>0){
               TreeNode cur=myque.poll();
                if(cur.left!=null){
                    myque.offer(cur.left);
                }
                if(cur.right!=null){
                    myque.offer(cur.right);
                }
                len--;
            }
        }
        return res;
    }
}


http://www.kler.cn/news/323270.html

相关文章:

  • 代码为笔,合作作墨,共绘共赢画卷———未来之窗行业应用跨平台架构
  • 【论文阅读】StoryMaker | 更全面的人物一致性开源工作
  • element-plus中日历组件设置起始为周一
  • git配置ssh免密
  • 【JavaEE】——多重锁,死锁问题和解决思路
  • vue3学习记录-computed
  • OJ在线评测系统 后端判题机架构搭建 使用原生实现Java安全管理器环境隔离
  • python用两类循环嵌套打印正置九九乘法口诀表和倒置九九乘法口诀表
  • 网络资源模板--Android Studio 图书借阅App
  • 基于Hive和Hadoop的电信流量分析系统
  • 网站建设中,营销型网站与普通网站有什么区别
  • 第四周做题总结_数据结构_栈与应用
  • 分页查询的优化
  • 小爱心换着玩
  • 【python】横截面数据分析及可视化报告示例
  • 拉格朗日插值讲解与MATLAB例程
  • (24)k8s部署mysql
  • django基于python的房价分析可视化系统的设计与开发 h1y0i
  • 洗浴中心澡堂污水处理设备主要包括以下几个步骤
  • 分享一下PHP基本语法总结
  • DERT目标检测源码流程图main.py的执行
  • 微信支付准备工作之内网穿透2024/9/28
  • 面向未来的设计:推动企业架构创新的关键——The Open Group 2024生态系统架构与可持续发展年度大会
  • 了解HTTPS
  • 如何在 Windows 台式机或笔记本电脑上恢复未保存的 Excel 文件
  • 【AI创作组】MATLAB基础语法总结
  • matlab处理语音信号
  • scikit-sparse安装
  • 【LLM多模态】文生视频综述From Sora What We Can See: A Survey of Text-to-Video Generation
  • 万户OA-ezOFFICE fileUpload.controller 任意文件上传漏洞复现