[剑指 offer] JT18---The mirror image of the binary tree (the mirror image of the tree is also recursive)

Sword Finger Offer Question 18

The topic is as follows

Insert picture description here

Idea and code

The tree is about supporting the exchange will be able to
exchange usesRecursion, divide and conquerthought of

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
    
    
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pRoot TreeNode类 
     * @return TreeNode类
     */
    TreeNode* Mirror(TreeNode* pRoot) {
    
    
        // write code here
        if(pRoot==NULL)
            return pRoot;
        else
            swap(pRoot->left,pRoot->right);
        Mirror(pRoot->left);
        Mirror(pRoot->right);
        return pRoot;
    }
};

Guess you like

Origin blog.csdn.net/qq_42136832/article/details/114519628