2017-02-26 77 views

回答

4
f(node, level): 
    if node is null then 
     return { Inf, -1 } 
    if isLeaf(node) then 
     return { level, node.value } 
    fleft <- f(node.left, level + 1) 
    fright <- f(node.right, level + 1) 
    fnode <- { min(fleft.first, fright.first), 0 } 
    if fnode.first = fleft.first then 
     fnode.second <- fnode.second + fleft.second 
    if fnode.first = fright.first then 
     fnode.second <- fnode.second + fright.second 
    return fnode 

甲函數返回一對其中first是最小葉級別,second是此級別葉子元素的總和。