https://leetcode.com/problems/count-complete-tree-nodes Easy

Решение

class Solution {
    fun countNodes(root: TreeNode?): Int {
        var l = root
        var r = root
        var lh = 0
        var rh = 0
        while (l != null) { lh++; l = l.left }
        while (r != null) { rh++; r = r.right }
        if (lh == rh) return (1 shl lh) - 1
        return 1 + countNodes(root?.left) + countNodes(root?.right)
    }
}