1161. Maximum Level Sum of a Binary Tree

时间:2019-09-27
本文章向大家介绍1161. Maximum Level Sum of a Binary Tree,主要包括1161. Maximum Level Sum of a Binary Tree使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
import java.util.*
import kotlin.collections.HashMap

/**
 * 1161. Maximum Level Sum of a Binary Tree
 * https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/
 * */
class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

class Solution {
    fun maxLevelSum(root: TreeNode?): Int {
        var level = 0
        var max = 0
        val map = HashMap<Int, Int>()
        val stack = Stack<TreeNode>()
        stack.add(root)
        while (!stack.isEmpty()) {
            level++
            var oneLevelSum = 0
            val size = stack.size
            for (i in 0 until size) {
                val node = stack.get(0)
                stack.removeAt(0)
                oneLevelSum += node.`val`
                if (node.left != null) {
                    stack.add(node.left)
                }
                if (node.right != null) {
                    stack.add(node.right)
                }
            }
            map.put(oneLevelSum, level)
            max = Math.max(max, oneLevelSum)
            
        }
       for (key in map.keys) {
            if (key == max) {
                level = map.get(key)!!
                break
            }
        }
        return level
    }
}                        

原文地址:https://www.cnblogs.com/johnnyzhao/p/11595459.html