zoukankan      html  css  js  c++  java
  • 1302. Deepest Leaves Sum

    package LeetCode_1302
    
    import java.util.*
    
    /**
     * 1302. Deepest Leaves Sum
     * https://leetcode.com/problems/deepest-leaves-sum/
     * Given a binary tree, return the sum of values of its deepest leaves.
     *
     * Constraints:
    1. The number of nodes in the tree is between 1 and 10^4.
    2. The value of nodes is between 1 and 100.
     * */
    class TreeNode(var `val`: Int) {
        var left: TreeNode? = null
        var right: TreeNode? = null
    }
    
    class Solution {
        /*
        * solution: BFS, calculating sum of tree by level by level, there is the result when reach last level,
        * Time complexity: O(n), Space complexity: O(n)
        * */
        fun deepestLeavesSum(root: TreeNode?): Int {
            if (root == null) {
                return 0
            }
            var result = 0
            val queue = LinkedList<TreeNode>()
            queue.offer(root)
            while (queue.isNotEmpty()) {
                val size = queue.size
                //reset for calculating for next level
                result = 0
                for (i in 0 until size) {
                    val cur = queue.pop()
                    result += cur.`val`
                    if (cur.left != null) {
                        queue.offer(cur.left)
                    }
                    if (cur.right != null) {
                        queue.offer(cur.right)
                    }
                }
            }
            return result
        }
    }
  • 相关阅读:
    springboot(6)-热部署
    springboot(5)-文件上传
    ubuntu修改tomcat使用的jdk
    jstack使用
    HeapAnalyzer如何使用
    查看linux下tomcat使用的jdk版本
    ubuntu-tomcat安装目录
    vi命令
    211java-jdk安装记录
    linux的java安装目录
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13955131.html
Copyright © 2011-2022 走看看