非递归版本:
/* 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) { //用栈 if(pRoot==NULL) { return; } stack<TreeNode*> jiedian; jiedian.push(pRoot); while(jiedian.size()!=0) { TreeNode* cur=jiedian.top(); jiedian.pop(); if(cur->right!=NULL||cur->left!=NULL) { TreeNode* temp=cur->left; cur->left=cur->right; cur->right=temp; } if(cur->left!=NULL) { jiedian.push(cur->left); } if(cur->right!=NULL) { jiedian.push(cur->left); } } } };