标签:page tree div www tar nod 二叉树 要求 ref
题目:二叉树的镜像
要求:操作给定的二叉树,将其变换为源二叉树的镜像。
/* 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) { } };
解题代码:
1 class Solution { 2 public: 3 void Mirror(TreeNode *pRoot) { 4 if(pRoot == nullptr) 5 return ; 6 if(pRoot->left == nullptr && pRoot->right == nullptr) 7 return ; 8 9 TreeNode* temp = pRoot->left; 10 pRoot->left = pRoot->right; 11 pRoot->right = temp; 12 13 if(pRoot->left != nullptr) 14 Mirror(pRoot->left); 15 if(pRoot->right != nullptr) 16 Mirror(pRoot->right); 17 } 18 };
标签:page tree div www tar nod 二叉树 要求 ref
原文地址:https://www.cnblogs.com/iwangzhengchao/p/9870161.html