C++ using queue, 0ms


  • 0
    S

    class Solution {
    public:

    TreeNode* invertTree(TreeNode* root) {  
        if (root==nullptr) return nullptr;
        
        queue<TreeNode*> st;
        st.push(root);
        
        while(!st.empty()){
            TreeNode* n=st.front();
            st.pop();
            
            if (n->left!=NULL)
                st.push(n->left);
            if (n->right!=NULL)
                st.push(n->right);
            swap(n->left,n->right);
            
        }
        return root;       
    }
    

    };


Log in to reply
 

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