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

二叉树题目

二叉树每层最大值

import java.util.*;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class Solution {
    public List<Integer> max_num(TreeNode root) {
        List<Integer> result = new ArrayList<>();  // 存储每层的最大值
        if (root == null) {
            return result;  // 如果树为空,返回空列表
        }
        
        Queue<TreeNode> queue = new LinkedList<>();  // 初始化队列
        queue.offer(root);  // 加入根节点
        
        while (!queue.isEmpty()) {  // 判断队列是否为空
            int levelSize = queue.size();  // 当前层的节点数
            int maxVal = Integer.MIN_VALUE;  // 当前层的最大值,初始化为最小整数值
            
            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();  // 取出队首节点
                maxVal = Math.max(maxVal, node.val);  // 更新当前层的最大值
                
                // 将左右子节点加入队列
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            
            result.add(maxVal);  // 将当前层的最大值加入结果
        }
        
        return result;
    }
}

二叉树每层平均值

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Double>averageOfLevels(TreeNode root){
        List<Double> result = new ArrayList();
        if (root==null){
            return result;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int l = queue.size();
            double sum = 0;

            for(int i=0;i<l;i++){
                TreeNode node = queue.poll();
                sum+=node.val;

                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }

            result.add(sum/l);

            
        }

        return result;

        
    }
}

平衡二叉树

class Solution {
    public boolean isBalanced(TreeNode root) {
    if (root == null) {
        return true;
    }
    
    
    int leftHeight = getHeight(root.left);
    
    int rightHeight = getHeight(root.right);
    
   
    if (Math.abs(leftHeight - rightHeight) > 1) {
        return false;
    }
    
   
    return isBalanced(root.left) && isBalanced(root.right);
}

private int getHeight(TreeNode node) {
    if (node == null) {
        return 0;
    }
    return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
}
}

二叉树对称

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;

        }

        return isMirror(root.left,root.right);
        
    }

    public boolean isMirror(TreeNode left,TreeNode right){
        if(left==null && right==null){
            return true;
        }

        if(left==null || right==null){
            return false;
        }
        if(left.val!=right.val){
            return false;
        }

        return isMirror(left.left,right.right) && isMirror(left.right,right.left);




    }
}

相关文章:

  • 冷热数据分层存储:提高效率与降低成本
  • 数据结构(泛型)
  • 大模型MCP协议与Function Calling:构建更智能的AI生态系统
  • 大模型微调中显存占用和训练时间的影响因素
  • OTP单片机调试工具之—单线数据编码
  • RCore学习记录001
  • 微信小程序threejs三维开发
  • 如何解决pymilvus中offset参数不生效的问题?
  • AI与人的智能,改变一生的思维模型【7】易得性偏差
  • 在 WSL中批量执行InSAR任务-stackSentinel.py
  • MySQL数据库知识总结
  • Redis7——进阶篇(六)
  • 小脑萎缩会致命吗?
  • Vue Router 中的导航守卫是什么?
  • 有了大语言模型还需要 RAG 做什么
  • AP AR
  • 二叉树_4_面试题汇总
  • AlphaGo 家族:从「偷看棋谱」到「自创宇宙套路」的 1008 天
  • 神经网络的基本知识
  • 生态安全的范式
  • 专利申请全球领先!去年我国卫星导航与位置服务产值超5700亿元
  • 2025全球城市科技传播能力指数出炉,上海位列第六
  • 气急败坏!20多名台湾艺人被台当局列为“重点核查对象”
  • 篮球培训机构东方启明星被指停摆,家长称已登记未退费用超百万
  • 尹锡悦宣布退出国民力量党
  • 海昏侯博物馆展览上新,“西汉帝陵文化展”将持续展出3个月