Two solutions - cpp solutions


  • 0
    C
    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    *     int val;
    *     TreeNode *left;
    *     TreeNode *right;
    *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    * };
    */
    
    
    struct TreeNode* findforme(struct TreeNode* r){
    if(r==NULL)return r;
    struct TreeNode* T=r->left;
    r->left=gobitches(r->right);
    r->right=gobitches(T);
    return r;
    }
    class Solution {
    public:
    TreeNode* invertTree(TreeNode* root) {
        struct TreeNode* ret=findforme(root);
        return ret;
      }
    };
    
    
    
      /////////////////////////////////////////////////// Second method////////////////////////////////
    
    
    
    /**
    * 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 root;
        swap(root->left,root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
      }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.