【树】面试题27. 二叉树的镜像

题目:

 

 

 

解法:

本题与主站 226 题相同:https://leetcode-cn.com/problems/invert-binary-tree/

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* mirrorTree(TreeNode* root) 
13     {
14         if (NULL == root) 
15         {
16             return root;
17         }
18         TreeNode *right = mirrorTree(root->right);
19         TreeNode *left = mirrorTree(root->left);
20         root->left = right;
21         root->right = left;
22         return root;
23 
24     }
25 };

 

posted @ 2020-05-03 16:15  梦醒潇湘  阅读(133)  评论(0)    收藏  举报