[Daily question] 38: Mirror image of binary tree

Title description

Operate the given binary tree and transform it into a mirror image of the source binary tree.

Enter description:

Mirror definition of binary tree: source binary tree

	    8
	   /  \
	  6   10
	 / \  / \
	5  7 9 11

Mirror binary tree

	    8
	   /  \
	  10   6
	 / \  / \
	11 9 7  5

Recursive thinking

Answer code:

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

If you have different opinions, please leave a message to discuss ~~

Published 152 original articles · praised 45 · 10,000+ views

Guess you like

Origin blog.csdn.net/AngelDg/article/details/105421470