NC72—二叉樹的映象

fkuner發表於2020-11-29

題意

操作給定的二叉樹,將其變換為源二叉樹的映象

題解

後序遍歷

/*
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 *pRoot) {
        recur(pRoot);
    }
    
    TreeNode* recur(TreeNode* pRoot) {
        if(pRoot == nullptr) return nullptr;
        
        TreeNode* left = recur(pRoot->left);
        TreeNode* right = recur(pRoot->right);
        
        pRoot->left = right;
        pRoot->right = left;
        
        return pRoot;
    }
};

相關文章