zoukankan      html  css  js  c++  java
  • [LeetCode#117]Populating Next Right Pointers in Each Node II

    The problem:

    Follow up for problem "Populating Next Right Pointers in Each Node".

    What if the given tree could be any binary tree? Would your previous solution still work?

    Note:

    • You may only use constant extra space.

    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
    
    Show Tags
    My first solution:
    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null)
                return;
            Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode> ();
            int cur = 1;
            int next = 0;
            queue.offer(root);
            
            while(queue.size() > 0) {
                TreeLinkNode temp = queue.poll();
                cur--;
                if (temp.left != null) {
                    queue.offer(temp.left);
                    next++;
                }
                if (temp.right != null) {
                    queue.offer(temp.right);
                    next++;
                }
                if (cur != 0) {
                    temp.next = queue.peek();
                } else{
                    temp.next = null;
                    cur = next;
                    next = 0;
                }
            }
        }
    }

    Ugly try:

    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null)
                return;
            int cur = 1;
            int next = 0;
            TreeLinkNode cur_node = null;
            TreeLinkNode pre_node = null;
            TreeLinkNode next_start = root;
            
            while (cur != 0) { //if cur == 0, it indicate next layer has no node.
                if (pre_node == null) { //a new level
                    cur_node = next_start;
                    pre_node = cur_node;
                    next_start = null;
                } else {
                    cur_node = cur_node.next;
                }
                cur--;
                if (cur_node.left != null) { //note the pre_node indicate the the node at next level
                    if (next_start == null) { 
                        next_start = cur_node.left;
                        pre_node = cur_node.left;
                        next++;
                    } else{ //since the next_start is not null, it must have a pre node before it in next level. 
                        pre_node.next = cur_node.left;
                        pre_node = cur_node.left;
                        next++;
                    }
                }
                if (cur_node.right != null) {
                    if (next_start == null) {
                        next_start = cur_node.right;
                        pre_node = cur_node.right;
                        next++;
                    } else{
                        pre_node.next = cur_node.right;
                        pre_node = cur_node.right;
                        next++;
                    }
                }
                if (cur == 0) {
                    pre_node.next = null;
                    cur = next;
                    next = 0;
                    pre_node = null;
                }
            }
        }
    }
     The improved analysis:
    This question is not easy, but could be solved in a very elegant way. 
    The traditional way of achieving level traversal over a tree is to use a Queue. However, in this problem, we are allowed to use constant space. It means we could not use Queue or other container any more.What's the solution?
    Think carefully about why we need to use a Queue in level traversal? 
    It because at each level we don't now the next element!
    That's cool. In this problem, we have next attribute in TreeLinkNode, can we use it to achieve the goal?
    Absolutely yes!
    
    Key idea: <treat the current level as list!!! It could be very simple!>
    At each level, we mainipulate on the next layer(update next pointer), to make it able to use as queue!
    Note: at each level, we equal to mainipulate on a linked list. we no longer need to use the ugly counting mechanism! the null pointer is the best way to control traversal!
    
    Invariant:
    1. we get the first node of current level from last_head;
    while (last_head != null) {
                TreeLinkNode cur = last_head;
                ...
    }
    
    2. Now we can work on the current level. using the control condition:
    while (cur != null) {
        ...
    }
    At current level, the thing we are caring about is next level, since the previous level have already updated the current level.
    2.1 we check each node's left child and right child. 
        2.1.1 once the left child is not null and the next_level's first node is not recorded, the left child is the head of next level's node.(which also means the current level's pre pointer is null)
        if (next_head == null) { 
            next_head = cur.left;
            pre = next_head;
        } 
        2.1.2 once the next_level's first node is recorded, it means the left child has pre node in the same level. 
        ...
        else{
            pre.next = cur.left;
            pre = pre.next;
        }
        2.1.3 the analysis over right child is the same as the above analysis.
    
    3. After we fininsh traversaling the current level, we should update relate varible for next level:
    last_head = next_head;
    next_head = null; 

    The improved solution:

    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null)
                return;
            TreeLinkNode last_head = root;
            TreeLinkNode next_head = null;
            TreeLinkNode pre = null;
            while (last_head != null) {
                TreeLinkNode cur = last_head;
                while (cur != null) {
                    if (cur.left != null) {
                        if (next_head == null) { 
                            //we have not reach the first node of next level yet, thus ther is no pre node currently
                            next_head = cur.left;
                            pre = next_head;
                        } else{
                            pre.next = cur.left;
                            pre = pre.next;
                        }
                    }
                    if (cur.right != null) {
                        if (next_head == null) {
                            next_head = cur.right;
                            pre = next_head;
                        } else{
                            pre.next = cur.right;
                            pre = pre.next;
                        }
                    }
                    cur = cur.next;
                }
                last_head = next_head;
                next_head = null; 
            }
        }
    }
  • 相关阅读:
    java mail使用qq邮箱发邮件的配置方法
    (利用tempdata判断action是直接被访问还是重定向访问)防止微信活动中用户绕过关注公众号的环节
    判断浏览器为微信浏览器
    解决表单(搜索框)回车的时候直接提交了表单不运行js的问题
    传智播客JavaWeb day11--事务的概念、事务的ACID、数据库锁机制、
    传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤
    页面上常用的一些小功能--QQ、回到顶部
    手机端禁止网页页面放大代码
    Resharp注册码
    NueGet设置package Source
  • 原文地址:https://www.cnblogs.com/airwindow/p/4259359.html
Copyright © 2011-2022 走看看