leetcode每日刷题计划-简单篇day26

今天还在肝大作业,仍然是选了简单题中的简单题,仅维持打卡

一遍过

Num 226 翻转二叉树

/**
 * 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* invertTree(TreeNode* root) {
        if(root==NULL) return NULL;
        if(root->left==NULL && root->right==NULL) return root; 
        TreeNode*temp=new TreeNode(0);
        temp->left=root->left;
        root->left=invertTree(root->right);
        root->right=invertTree(temp->left);
        return root;
    }
};
View Code
时间才能证明一切,选好了就尽力去做吧!
原文地址:https://www.cnblogs.com/tingxilin/p/11192433.html