Leetcode刷题笔记(c++)_热题 HOT 100_226. 翻转二叉树

递归

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root||(!root->left&&!root->right))return root;
        auto temp=root->left;
        root->left=invertTree(root->right);
        root->right=invertTree(temp);
        return root;
    }
};

你可能感兴趣的:(leetcode,c++,算法)