117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II

Bfs (mle)

//
public class Solution {
    public void connect(TreeLinkNode root) {
      if(root == null) return;
      Queue<TreeLinkNode> queue = new LinkedList<>();
      queue.offer(root);
      while(!queue.isEmpty()){
        int size = queue.size();
        TreeLinkNode cur = queue.poll();
        for(int i = 0; i < size; i++){
          if(i != size - 1){
            cur.next = queue.peek();
          }
          if(cur.left != null) queue.offer(cur.left);
          if(cur.right != null) queue.offer(cur.right);
        }
      }
    }
}

      
* You may only use constant extra space.
* Recursive approach is fine, implicit stack space does not count as extra space for this problem.




/// youtube , which I understand 
https://www.youtube.com/watch?v=mBVN8G-pIzU




public class Solution {
    public void connect(TreeLinkNode parent) {
      TreeLinkNode child = null;
      TreeLinkNode childHead = null;
      
      while(parent != null){ // a new level
        while(parent != null){    // stay in the same level, parent != null
          
          if(parent.left != null){   // parent has left child 
            if(childHead == null){     // a very fresh start in this new level, we havent set the child head yet ( also havent set child )
              childHead = parent.left;  // set new start for child head and child 
              child = parent.left;
            }else{                      // we already started in this level, so the childhead != null. 
              child.next = parent.left;    // parent has a left child . we connect child to parent.left
              child = child.next;          // move child to current position 
            }
          }
          
          
          
          if(parent.right != null){    // parent has right child 
            if(childHead == null){         // a very fresh start in this new level, we havent set the child head yet ( also havent set child )
              childHead = parent.right;      // set new start for child head and child 
              child = parent.right;
            }else{                // we already started in this level, so the childhead != null. 
              child.next = parent.right;     // parent has a right child . we connect child to parent.right 
              child = child.next;            // move child to current position 
            }
          }
          
          parent = parent.next;         // after checking parent.left, parent.right, we can move parent to parent.next 
        }                       //  if parent.next != null, we are still in the same level , carry on in the second while loop 
        
        
        
           //  if parent.next == null, then we need to move down a level, so we are out of the second while loop,
        parent = childHead;    // move the parent from root to childhead 
        child = null;  // set child to null, prepare to move to a new level . set it to null because we dont know the tree strucuture, where its gonna be 
        childHead = null;       //  set childhead to null, prepare to move to a new level 
      }
        
    }
}



// for the last level, when parent is set to childhead, and child = null, childhead = null. at this moment, parent is not null yet 
// we go thru the second while loop, parent.left == null, parent.right == null, parent = parent.next , which is not null again, we go thru 
// the second while loop again, and parent.left == null, parent.right == null,  parent = parent.next ... until we reach the end of this level, 
// parent = null, so we are out of the second while loop, set parent = childhead, and rememeber the childhead is null , so parent is null, so we 
// are out of the first while loop. 

 

Given a binary tree

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.

Example:

Given the following binary tree,

     1
   /  \
  2    3
 / \    \
4   5    7

After calling your function, the tree should look like:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \    \
4-> 5 -> 7 -> NULL

posted on 2018-08-09 17:02  猪猪&#128055;  阅读(75)  评论(0)    收藏  举报

导航