JZ18 二叉树的镜像
本文最后更新于:2022年4月9日 中午
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
class Solution { public:
TreeNode* Mirror(TreeNode* pRoot) { if (pRoot == nullptr) return nullptr; stack<TreeNode *> stack; TreeNode *node = pRoot; while (node || !stack.empty()) { while (node) { stack.push(node); node = node->left; } node = stack.top(); stack.pop(); swap(node->left, node->right); node = node->left; } return pRoot; } };
|
| class Solution { public: TreeNode* Mirror(TreeNode* pRoot) { if (pRoot == nullptr) return nullptr; Mirror(pRoot->left); Mirror(pRoot->right); swap(pRoot->left, pRoot->right); return pRoot; } };
|