Inverting a binary tree
Here you are creating a node unnecessarily: 


   TreeNode* tn = new TreeNode(0);
    tn=root-&gt;right;



You could write simpler as: 

   TreeNode* tn = root-&gt;right;


Because you're not using the original value of tn anyway, you only need it as a work variable for swapping the left and right nodes.

Also, your implementation doesn't use the return value of the recursive invertTree calls. You can use them better and get rid of the conditional statements, making the solution more compact:

TreeNode* invertTree(TreeNode* root) {
    if (root == NULL) {
        return NULL;
    }

    TreeNode* work = root-&gt;right;
    root-&gt;right = invertTree(root-&gt;left);
    root-&gt;left = invertTree(work);
    return root;
}



   I think I am invoking undefined behavior when the control hits the leaf nodes.


No, you're not invoking "undefined behavior" when hitting the leaf nodes. Actually I think you meant to say intermediary nodes. At leaf nodes, the function receives a null node and you return null. At intermediary nodes, you allocate an unnecessary node, as explained earlier. The unnecessary allocation doesn't trigger an undefined behavior, but since you never free the allocated memory, this is a memory leak, and indeed a problem if the program was used for a substantial amount of time, accumulating unreleased memory.