invert a binary tree.
4 / 2 7 / / 1 3 6 9
to
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; }