【二叉树】515. 在每个树行中找最大值

在每个树行中找最大值

给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
请添加图片描述

请添加图片描述

/**
 * 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) {
        if(root == null){
            return new ArrayList();
        }

        List<Integer> ans = new ArrayList();
        Queue<TreeNode> queue = new LinkedList();
        Queue<TreeNode> nextLevel = new LinkedList();
        int maxVal = root.val;
        TreeNode cur = root;
        queue.offer(cur);

        while(!queue.isEmpty()){
            cur = queue.poll();
            maxVal = maxVal > cur.val ? maxVal : cur.val;
            if(cur.left!=null){
                nextLevel.offer(cur.left);
            }
            if(cur.right!=null){
                nextLevel.offer(cur.right);
            }

            if(queue.isEmpty()){
                ans.add(maxVal);
                if(!nextLevel.isEmpty()){
                TreeNode tmp = nextLevel.peek();
                maxVal = tmp.val;
                }
                queue = nextLevel;
                nextLevel = new LinkedList();
            }
        }

        return 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<Integer> largestValues(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        if (root == null) {
            return result;
        }

        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);

        // 层序遍历
        while (!q.isEmpty()) {
            int sz = q.size();
            // 在每一层维护一个变量,用于存储最大值
            int max = Integer.MIN_VALUE;
            // 对每一层的元素进行遍历
            for (int i = 0; i < sz; i++) {
                TreeNode cur = q.poll();
                // 每取出来一个元素就进行一次比较
                max = Math.max(max, cur.val);

                // 添加下一层的元素
                if (cur.left != null) {
                    q.offer(cur.left);
                }
                if (cur.right != null) {
                    q.offer(cur.right);
                }
            }
            // 将每一层的最大值添加到结果变量中
            result.add(max);
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值