C++ easy recursive solution


  • 8
    J
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if(!root)
                return;
            if(root->left)
                root->left->next = root->right;
            if(root->next && root->right)
                root->right->next = root->next->left;
            connect(root->left);
            connect(root->right);
        }
    };

  • 0
    L

    There is no need to add root->right in if(), since 'You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children)'.


Log in to reply
 

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