/** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree(TreeNode *root) { if (root == NULL) return; invertBinaryTree(root->left); invertBinaryTree(root->right); TreeNode* temp = root->left; root->left = root->right; root->right = temp; }
/** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree(TreeNode *root) { // write your code here if (NULL == root) { return; } invertBinaryTree(root->left); invertBinaryTree(root->right); TreeNode *temp = root->left; root->left = root->right; root->right = temp; }
/** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree(TreeNode *root) { // write your code here // No return in this case have to check NULL. if (!root) return; invertBinaryTree(root->left); invertBinaryTree(root->right); // Using the C++ convinient swap function. swap(root->left, root->right); return; }
/** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree_recursive(TreeNode *root) { // write your code here if (root->right == NULL && root->left == NULL) return; TreeNode *temp = root->left; root->left = root->right; root->right = temp; if (root->right != NULL) invertBinaryTree(root->right); if (root->left != NULL) invertBinaryTree(root->left); }
/** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ void invertBinaryTree(TreeNode *root) { if (root == NULL) { return; } TreeNode *tmp = root->left; root->left = root->right; root->right = tmp; invertBinaryTree(root->left); invertBinaryTree(root->right); }