*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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,959评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 23周岁了,可以有的就是一个不服输的心了。今天对于练字突然有了点豁然开朗的感觉,有点激动。 祝我生日快乐,爸爸妈妈...
    顾陌涵阅读 263评论 0 0
  • 周末等在候车厅,对面的小姑娘坐在军绿色的行李包上对我眨巴眨巴眼睛,我的心有些柔软。 想起包里有袋小虾,掏出来递给了...
    义赛阅读 380评论 4 4
  • 内心的哀鸣 wxg 世间自有万种风情 每每想起 自觉不得安宁 踏青驳船抚长风 折枝弄影倚凉亭 人对花自怜 花对人空...
    silencesky阅读 227评论 0 1