Do we have test case like as follows?


  • 1
    G
              1
           /    \
          2       3
        /  \      /  \
       4    5    6    7
      / \       / \  / \
     7   8     9  10 11 12
    

    I believe my code which has passed all test will not connect the 8 and 9, although the tree satisfy the condition that "You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children)." Let me know if I misunderstand this.

    void connect(TreeLinkNode *root) {
        if(root==NULL) return;
        
        if(root->left)
          root->left->next = root->right;
        if(root->right)
          root->right->next = (root->next) ? root->next->left:NULL;
        connect(root->left);
        connect(root->right);
    }

  • 3
    T

    This is not a perfect binary tree. The node with value 5 is a leaf, but it is not on the same level as all the other leaves.


Log in to reply
 

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