翻转二叉树
解析:这道题的树并不是二叉查找树,所以对每个结点的左右结点交换即可。
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
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 ;
invertBinaryTree(root->left);//后续遍历,先对左右结点操作
invertBinaryTree(root->right);
TreeNode *tmp;
tmp=root->left;
root->left=root->right;
root->right=tmp;
}
};