题目:翻转二叉树
问题描述:翻转一棵二叉树。
思路:如果跟不为空,则使同一个双亲的左孩子和右孩子调换位置。
代码:
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode *root) {
// write your code here
if(root==NULL) return;
TreeNode *temp=root->left;
root->left=root->right;
root->right=temp;
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
};
感想:做题一定要注意函数的返回类型,不要按自己想的去做,不然会做很多无用功的。