Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
639 B
2
Indexable
Never
function isValueUnique(node: TreeNode, subNode?: TreeNode): boolean {
    if (!subNode) {
        return true;
    }

    if (node.value === subNode.value && node !== subNode) {
        return false;
    }

    return isValueUnique(node, subNode.leftChild) && isValueUnique(node, subNode.rightChild);
}

function isNodeUnique(node?: TreeNode): TreeNode | null {
    if (!node) {
        return null;
    }

    const isUnique = isValueUnique(node, root);
    if (isUnique) {
        return node;
    }

    return isNodeUnique(node.leftChild) || isNodeUnique(node.rightChild);
}

console.log(isNodeUnique(root));