zoukankan      html  css  js  c++  java
  • 515. 在每个树行中找最大值

    您需要在二叉树的每一行中找到最大的值。

    示例:

    输入: 
    
              1
             / 
            3   2
           /      
          5   3   9 
    
    输出: [1, 3, 9]
    
    在真实的面试中遇到过这道题?
    class Solution {
        public List<Integer> largestValues(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Queue<TreeNode> queue = new LinkedList<>();
            if(root == null) return res;
            queue.add(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                int Max = Integer.MIN_VALUE;
                for(int i=0; i<size; i++){
                    TreeNode node = queue.poll();
                    if(node.val > Max) Max = node.val;
                    if(node.left != null) queue.add(node.left);
                    if(node.right != null) queue.add(node.right);
                }
                res.add(Max);
            }
            return res;
        }
    }
  • 相关阅读:
    Linux 系统启动过程
    Linux启动U盘制作
    JSONP 教程
    JSON 使用
    JSON.stringify()
    JSON.parse()
    Apache模块开发指南-APR池
    [C++基础]goto的用法
    atexit()函数
    c++ good books
  • 原文地址:https://www.cnblogs.com/Roni-i/p/10510237.html
Copyright © 2011-2022 走看看