Why my code shows run time error?


  • 0
    K
    /**
     * 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->left==NULL || root->right==NULL) return root;
            TreeNode* newLeft=invertTree(root->left);
            TreeNode* newRight=invertTree(root->right);  
            TreeNode* tmpNode=root->right;
            root->right=newLeft;
            root->left=tmpNode;
            return root;
        }
    };

  • 0
    Q

    If root is NULL, your code will fail.


Log in to reply
 

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