zoukankan      html  css  js  c++  java
  • Subtree with Maximum Average

    我写的

    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
    public class Solution {
        /**
         * @param root the root of binary tree
         * @return the root of the maximum average of subtree
         */
        private TreeNode node;
        private double maxAverage = Double.MIN_VALUE;
        
        public TreeNode findSubtree2(TreeNode root) {
            // Write your code here
            
            helper(root);
            return node;
            
        }
        private double helper(TreeNode root) {
            if (root == null) {
                return 0;
            }
            //改成double
            double left = helper(root.left);
            double right = helper(root.right);
            double sum = root.val + left + right;
            int n = count(root);
            if (sum / n > maxAverage) {
                maxAverage = sum / n;
                node = root;
            }
            return sum;
        }
        private int count(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftcount = count(root.left);
            int rightcount = count(root.right);
            return leftcount + rightcount + 1;
        }
    }
    View Code
  • 相关阅读:
    selenium自动化测试实战——12306铁路官网范例
    mock接口开发——flask模块
    python调用接口——requests模块
    python操作redis
    python修改excel内容
    python读excel
    python发送邮件
    python写日志
    python的模块
    python写excel
  • 原文地址:https://www.cnblogs.com/yunyouhua/p/6741112.html
Copyright © 2011-2022 走看看