标签:
二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5
class Solution { public: //栈的非递归 void Mirror(TreeNode *pRoot) { if (pRoot == NULL)return; stack<TreeNode*> st; TreeNode* p = NULL; st.push(pRoot); while (st.size()) { p = st.top(); st.pop(); swap(p->left, p->right); if (p->left)st.push(p->left); if (p->right)st.push(p->right); } } //队列的非递归 void Mirror(TreeNode *pRoot) { if (pRoot == NULL)return; queue<TreeNode*> qu; TreeNode* p = NULL; qu.push(pRoot); while (qu.size()) { p = qu.front(); qu.pop(); swap(p->left, p->right); if (p->left)qu.push(p->left); if (p->right)qu.push(p->right); } } //递归 void Mirror(TreeNode *pRoot) { if (pRoot == NULL)return; swap(pRoot->left, pRoot->right); Mirror(pRoot->left); Mirror(pRoot->right); } };
标签:
原文地址:http://www.cnblogs.com/yuguangyuan/p/5903585.html