*LC-117 Populating Next Right Pointers in Each Node II

For 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
/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
//version 1
// classical BFS solution (using Queue)
public class Solution {
    public void connect(TreeLinkNode root) {
         if(root==null) {
             return;
         }
         Queue<TreeLinkNode> queue = new LinkedList<>();
         queue.add(root);
         while(!queue.isEmpty()){
             int size = queue.size();
             for(int i=0; i<size; i++){
                TreeLinkNode front = queue.poll();
                //second to last
                if(i<size-1){
                    front.next = queue.peek();
                }
                if(front.left!=null) {
                    queue.add(front.left);
                }
                if(front.right!=null) {
                    queue.add(front.right);
                }
             }
         }
    }
}
// version 1.2
// classical BFS solution (using two Queue for reference)
public class Solution {
    public void connect(TreeLinkNode root) {
        if( root == null) {
            return;
        }
           
        Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); 
        queue.offer(root);
        while (!queue.isEmpty()){
            Queue<TreeLinkNode> next = new LinkedList<TreeLinkNode>();
            int size = queue.size();
            for(int i = 0; i < size; i++) {
                 TreeLinkNode topNode = queue.poll();
                 //add pointer if next node in same level exist
                 if(queue.peek() != null) {
                      topNode.next = queue.peek();
                  }else {
                      topNode.next = null;
                  }
                  //add children to next level
                  if(topNode.left != null) {
                      next.offer(topNode.left);
                  }
                  if(topNode.right != null) {
                      next.offer(topNode.right);
                  }
            }
            queue = next;
        }
    }
}

//version 2
//hashmap solution 
public class Solution {
    public void connect(TreeLinkNode root) {
        HashMap<Integer,TreeLinkNode> map = new HashMap<Integer,TreeLinkNode>();
        traverse(root, map , 0);
    }
    private void traverse(TreeLinkNode root, HashMap<Integer,TreeLinkNode> map, int level){
        if(root == null) {
            return;
        }
        
        if (!map.containsKey(level)){
            map.put(level,root);
        } else {
            map.get(level).next = root;
            map.put(level, root);
        }
        level++;
        traverse(root.left, map, level);
        traverse(root.right, map, level);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,049評論 0 23
  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,901評論 0 33
  • 23周歲了,可以有的就是一個不服輸?shù)男牧恕=裉鞂τ诰氉滞蝗挥辛它c豁然開朗的感覺,有點激動。 祝我生日快樂,爸爸媽媽...
    顧陌涵閱讀 271評論 0 0
  • 周末等在候車廳,對面的小姑娘坐在軍綠色的行李包上對我眨巴眨巴眼睛,我的心有些柔軟。 想起包里有袋小蝦,掏出來遞給了...
    義賽閱讀 435評論 4 4
  • 內(nèi)心的哀鳴 wxg 世間自有萬種風情 每每想起 自覺不得安寧 踏青駁船撫長風 折枝弄影倚涼亭 人對花自憐 花對人空...
    silencesky閱讀 257評論 0 1

友情鏈接更多精彩內(nèi)容