代码随想录:翻转二叉树
就是遍历
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
tra(root);
return root;
}
void tra(TreeNode* target) {
if (target != NULL) {
TreeNode* temp = target->left;
target->left = target->right;
target->right = temp;
tra(target->left);
tra(target->right);
}
}
};
标签:right,TreeNode,target,val,nullptr,随想录,二叉树,翻转,left
From: https://www.cnblogs.com/huigugu/p/18662909