剑指 Offer 27. 二叉树的镜像(python/c++)

剑指 Offer 27. 二叉树的镜像(python/c++)
剑指 Offer 27. 二叉树的镜像(python/c++)
剑指 Offer 27. 二叉树的镜像(python/c++)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* mirrorTree(TreeNode* root) {
        if(!root){
            return NULL;
        }
        TreeNode*left = mirrorTree(root->left);
        TreeNode*right = mirrorTree(root->right);
        root->left = right;
        root->right = left;
        return root;
    }
};
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def mirrorTree(self, root: TreeNode) -> TreeNode:
        if not root:return
        root.left,root.right = self.mirrorTree(root.right),self.mirrorTree(root.left)
        return root
上一篇:剑指OFFER----面试题27. 二叉树的镜像


下一篇:cvte春招测试面试记录