invert a binary tree.
4 / \ 2 7 / \ / \ 1 3 6 9to
4 / \ 7 2 / \ / \ 9 6 3 1递归代码1: TreeNode* invertTree(TreeNode* root) { if(root==NULL) return ; if ( root->left != NULL) invertTree(root->left); if ( root->right != NULL) invertTree(root->right); TreeNode * ptmpNode = root->left; root->left = root-right ; root->right = ptmpNode ; }
递归代码2:
TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; TreeNode * ptmpNode = root->left; root->left = invertTree(root->right); root->right = invertTree(ptmpNode); return root; }
转载于:https://www.cnblogs.com/jasonkent27/p/4824844.html