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

二叉树练习

102. 二叉树的层序遍历 - 力扣(LeetCode)

使用队列进行层序遍历。

/**
 * 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<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            List<Integer>temp = new ArrayList<>();
            num = q.size();
            while(num != 0)
            {
                TreeNode t = q.poll();
                temp.add(t.val);
                if(t.left != null)
                {
                    q.offer(t.left);
                }
                if(t.right != null)
                {
                    q.offer(t.right);
                }
                num--;
            }
            ans.add(temp);
        }
        return ans;
    }
}

107. 二叉树的层序遍历 II - 力扣(LeetCode)

在上一题的基础上,翻转一下ans

/**
 * 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<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            List<Integer>temp = new ArrayList<>();
            num = q.size();
            while(num != 0)
            {
                TreeNode t = q.poll();
                temp.add(t.val);
                if(t.left != null)
                {
                    q.offer(t.left);
                }
                if(t.right != null)
                {
                    q.offer(t.right);
                }
                num--;
            }
            ans.add(temp);
        }
        Collections.reverse(ans);
        return ans;
    }
}

199. 二叉树的右视图 - 力扣(LeetCode)

/**
 * 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<Integer> rightSideView(TreeNode root) {
        List<Integer>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            num = q.size();
            while((num - 1) != 0)
            {
                TreeNode temp = q.poll();
                if(temp.left != null)
                {
                    q.offer(temp.left);
                }
                if(temp.right != null)
                {
                    q.offer(temp.right);
                }
                num--;
            }
            TreeNode t = q.poll();
            if(t.left != null)
            {
                q.offer(t.left);
            }
            if(t.right != null)
            {
                q.offer(t.right);
            }
            ans.add(t.val);
        }
        return ans;
    }
}

637. 二叉树的层平均值 - 力扣(LeetCode)

/**
 * 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>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            num = q.size();
            int tnum = num;
            long ad = 0;
            while(num != 0)
            {
                TreeNode temp = q.poll();
                if(temp.left != null)
                {
                    q.offer(temp.left);
                }
                if(temp.right != null)
                {
                    q.offer(temp.right);
                }
                ad += temp.val;
                num--;
            }
            double cur = (double)ad / (double)tnum;
            ans.add(cur);
        }
        return ans;
    }
}

429. N 叉树的层序遍历 - 力扣(LeetCode)

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>>ans = new ArrayList<>();
        if(root == null)
        {
            return ans;
        }
        Queue<Node>q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            List<Integer>temp = new ArrayList<>();
            int num = q.size();
            while(num != 0)
            {
                Node t = q.poll();
                temp.add(t.val);
                List<Node>list = t.children;
                for(Node n : list)
                {
                    q.offer(n);
                }
                num--;
            }
            ans.add(temp);
        }
        return ans;
    }
}

515. 在每个树行中找最大值 - 力扣(LeetCode)

/**
 * 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<Integer> largestValues(TreeNode root) {
        List<Integer>ans = new ArrayList<>();
        if(root == null)
        {
            return ans;
        }
        Queue<TreeNode>q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            int num = q.size();
            int max = Integer.MIN_VALUE;
            while(num != 0)
            {
                TreeNode temp = q.poll();
                if(temp.left != null)
                {
                    q.offer(temp.left);
                }
                if(temp.right != null)
                {
                    q.offer(temp.right);
                }
                max = max > temp.val ? max : temp.val;
                num--;
            }
            ans.add(max);
        }
        return ans;
    }
}

相关文章:

  • 垃圾短信分类
  • 深度学习篇---卷积网络结构
  • 23种设计模式-创建型模式-单例
  • WPF 与 C# 开发深度剖析
  • 如何避免权限分配不合理导致的信息安全风险?
  • 【天梯赛】L2-012(实战反思代码实现)
  • 压测工具开发(一)——使用Qt Designer构建简单界面
  • Java编程思想:为何有时要将子类对象赋值给父类引用
  • 六级备考 词汇量积累(day11)
  • DNS域名解析服务
  • 【区块链安全 | 第一篇】密码学原理
  • C++初阶入门基础二——类和对象(中)
  • 避免踩坑!查收查引常见问题解答
  • C语言动态内存管理深度解析与嵌入式开发实战
  • 12_JavaScript_实现日期
  • dfs(深度优先)——太抽象了
  • 【新能源汽车实验室设备控制:PLC与单片机选型指南(深度解析+实战案例)】
  • AI数字人直播系统
  • java中的枚举类型和c,c++的有区别吗?c,c++的枚举,结构体,联合体,三种数据有什么区别和联系
  • 基于DrissionPage的TB商品信息采集与可视化分析
  • 大环线呼之欲出,“金三角”跑起来了
  • 混乱的5天:俄乌和谈如何从充满希望走向“卡壳”
  • 雷军内部演讲回应质疑:在不服输、打不倒方面,没人比我们更有耐心
  • 黑龙江省政府副秘书长许振宇,拟任正厅级领导
  • 经常口干口渴的人,要当心这些病
  • 中国科学院院士、我国航天液体火箭技术专家朱森元逝世