Invert a binary tree.
Example:
Input:
4/ \2 7/ \ / \ 1 3 6 9
Output:
4/ \7 2/ \ / \ 9 6 3 1
题目链接:https://leetcode.com/problems/invert-binary-tree/
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void Invert(TreeNode *root){if(root){TreeNode *tmp=root->left;root->left=root->right;root->right=tmp;Invert(root->left);Invert(root->right);}}TreeNode* invertTree(TreeNode* root) {if(root){Invert(root);}return root;}
};