zoukankan      html  css  js  c++  java
  • 515. Find Largest Value in Each Tree Row查找一行中的最大值

    [抄题]:

    You need to find the largest value in each row of a binary tree.

    Example:

    Input: 
    
              1
             / 
            3   2
           /      
          5   3   9 
    
    Output: [1, 3, 9]

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不知道怎么确定每一行的大小:不熟悉bfs。其中q每次只存了一行,所以size就是当前数组的大小

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    Queue<TreeNode> q = new LinkedList<>(); 因为都可以随便动?

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    BFS要点:(3先生)先加头、先判Empty、先取长度

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    图是v+e 树就是点数n

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

    // package whatever; // don't place package name!
    
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    class TreeNode 
    {
      int val;
      TreeNode left, right;
      
      //parameter is another item
      TreeNode(int item) {
        val = item;
        left = right = null;
      }
    }
    
    
    class Solution {
        TreeNode root;
      
        public List<Integer> largestValues(TreeNode root) {
            //ini: q, int max, Array
            int max = Integer.MIN_VALUE;
            //implement by linkedlist
            Queue<TreeNode> q = new LinkedList<>();
            List<Integer> answer = new ArrayList<Integer>();
            
            //cc
            if (root == null) return answer;
            
            q.offer(root);
            while (!q.isEmpty()) {
                //
                int size = q.size();
                
                for (int i = 0; i < size; i++) {
                    TreeNode cur = q.poll();
                    max = Math.max(cur.val, max);
                    if (cur.left != null) q.offer(cur.left); 
                    if (cur.right != null) q.offer(cur.right);
                }
                //add to answer
                answer.add(max);
                //renew max
                max = Integer.MIN_VALUE;
        }
            
            return answer;
        }
    }
    
    class MyCode {
      public static void main (String[] args) {
        Solution tree = new Solution();
        tree.root = new TreeNode(1);
        tree.root.left = new TreeNode(2);
        tree.root.right = new TreeNode(3);
        tree.root.left.left = new TreeNode(4);
        tree.root.left.right = new TreeNode(5);
    
        //TreeNode t = tree.upsideDownBinaryTree(tree.root);
        List<Integer> answer = tree.largestValues(tree.root);
        int size = answer.size();
        for (int i = 0; i < size; i++) 
        System.out.println("answer[i] = " + answer.get(i));
      }
    }
    View Code

     [潜台词] :

  • 相关阅读:
    Datawhale编程实践(LeetCode 腾讯精选练习50)Task2
    Datawhale编程实践(LeetCode 腾讯精选练习50)Task1
    关于深度学习中样本权重取0的问题
    对多维numpy数组使用random.shuffle的问题
    Ubuntu18.04LTS左上角光标闪烁原因之一:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver.
    pycharm 报错ImportError: could not import module 'PySide2.QtWidgets'
    pyinstaller 打包pyside2项目遇到plugins window问题
    git pull的时候出错: Git Couldn't reserve space for cygwin's heap
    angularjs鼠标移入移出实现显示隐藏
    gulp编译出现Cannot find module 'internal/util/types'——node环境的变更
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9379977.html
Copyright © 2011-2022 走看看