Tuesday, May 12, 2015

kth in-order traversal node of binary tree

(node, k):
    if node == null || k < 0:
        throw
    return (node, k, i: {value = 0})

(n, k, i):
    if n == null:
        return null
    x = (n.left, k, i)
    if x != null:
        return x
    if i.value == k:
        return n
    i.value++
    x = (n.right, k, i)
    if x != null:
        return x
    return null

No comments:

Post a Comment