二叉树的镜像(简单,数)

题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
比如: 源二叉树
8
/
6 10
/ \ /
5 7 9 11
镜像二叉树
8
/
10 6
/ \ /
11 9 7 5
示例1
输入
{8,6,10,5,7,9,11}
返回值
{8,10,6,11,9,7,5}

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
void solve(TreeNode* root)
{
    
    
    if(root==NULL) return;
    TreeNode*tmp=root->left;
    root->left=root->right;
    root->right=tmp;
    solve(root->left);
    solve(root->right);
}
class Solution {
    
    
public:
    TreeNode* Mirror(TreeNode* pRoot) {
    
    
        solve(pRoot);
        return pRoot;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43540515/article/details/114293709