1302. Deepest Leaves Sum

#### QUESTION:

Given a binary tree, return the sum of values of its deepest leaves.

Example 1:

``````Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
Output: 15
``````

Constraints:

``````The number of nodes in the tree is between 1 and 10^4.
The value of nodes is between 1 and 100.
``````

#### EXPLANATION:

1. 创建两个list, listA,listB
2. 将第一个节点放入listA, 如果第一个节点有子节点, 那么我们就将子节点放入listB
3. 遍历listB,同时计算总和, 将b中节点的子节点放入listA
4. 当listA和listB都没有节点的时候,说明就是最后一层, 那么返回最后一次计算的结果就可以

#### SOLUTION:

``````class Solution {
fun deepestLeavesSum(root: TreeNode?): Int {
root?: return 0
var arrayA : ArrayList<TreeNode> = ArrayList()
var arrayB : ArrayList<TreeNode> = ArrayList()
var result = 0
while (!arrayA.isEmpty() || !arrayB.isEmpty()) {
result = 0;
var arrayI : ArrayList<TreeNode> = ArrayList()
var arrayJ : ArrayList<TreeNode> = ArrayList()
if (arrayA.isEmpty()) {
arrayI = arrayB
arrayJ = arrayA
}
if (arrayB.isEmpty()) {
arrayI = arrayA
arrayJ = arrayB
}
val iterator = arrayI.iterator()
while (iterator.hasNext()) {
val next = iterator.next()
result += next.`val`
iterator.remove()