zoukankan      html  css  js  c++  java
  • populating-next-right-pointers-in-each-node-ii

    /**
    * 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
    *
    * 跟进“在每个节点中填充下一个右指针”的问题。
    * 如果给定的树可以是任何二叉树呢?您以前的解决方案是否仍然有效?
    * 音符:
    * 您只能使用恒定的额外空间。
    * 例如,
    * 给定以下二叉树,
    * 1
    * /
    * 2 3
    * /
    * 4 5 7
    * 调用函数后,树应如下所示:
    * 1 -> NULL
    * /
    * 2 -> 3 -> NULL
    * /
    * 4-> 5 -> 7 -> NULL
    */

    /**
     * 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
     *
     * 跟进“在每个节点中填充下一个右指针”的问题。
     * 如果给定的树可以是任何二叉树呢?您以前的解决方案是否仍然有效?
     * 音符:
     * 您只能使用恒定的额外空间。
     * 例如,
     * 给定以下二叉树,
     *          1
     *        /  
     *       2    3
     *      /     
     *     4   5    7
     * 调用函数后,树应如下所示:
     *          1 -> NULL
     *        /  
     *       2 -> 3 -> NULL
     *      /     
     *     4-> 5 -> 7 -> NULL
     */
    
    public class Main45 {
        public static void main(String[] args) {
    
        }
    
        public class TreeLinkNode {
            int val;
            TreeLinkNode left, right, next;
            TreeLinkNode(int x) { val = x; }
        }
    
        public void connect(TreeLinkNode root) {
            if (root == null) {
                return ;
            }
            LinkedList<TreeLinkNode> link = new LinkedList<>();
            link.offer(root);
            while (!link.isEmpty()) {
                TreeLinkNode head = link.peek();
                link.poll();
                int size = link.size();
                if (head.left != null) {
                    link.offer(head.left);
                }
                if (head.right != null) {
                    link.offer(head.right);
                }
                while (size > 0) {
                    TreeLinkNode tree = link.peek();
                    link.poll();
                    head.next = tree;
                    head = tree;
                    if (tree.left != null) {
                        link.offer(tree.left);
                    }
                    if (tree.right != null) {
                        link.offer(tree.right);
                    }
                    size--;
                }
                head.next = null;
            }
        }
    
    }
    

      

  • 相关阅读:
    VMware Workstation 11 安装MAC OS X 10.10 Yosemite(14B25)图解 2015-01-13 12:26:01|
    tensor搭建--windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速
    vs2015终于配置完成了
    Visual Studio 2015 update 3各版本下载地址
    惊艳的cygwin——Windows下的Linux命令行环境的配置和使用
    TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派
    官方Caffe-windows 配置与示例运行
    ipython notebook 如何打开.ipynb文件?
    Ubuntu16.04 +cuda8.0+cudnn+caffe+theano+tensorflow配置明细
    【CUDA】CUDA开发环境搭建
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11338373.html
Copyright © 2011-2022 走看看