• 116. Populating Next Right Pointers in Each Node


    题目:

    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.
    • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

    For example,
    Given the following perfect binary tree,

             1
           /  
          2    3
         /   / 
        4  5  6  7
    

    After calling your function, the tree should look like:

             1 -> NULL
           /  
          2 -> 3 -> NULL
         /   / 
        4->5->6->7 -> NULL
    
    Hide Tags
     Tree Depth-first Search 

    链接:  http://leetcode.com/problems/populating-next-right-pointers-in-each-node/

    题解:

    使用DFS。题目给出完全二叉树,所以只要先判断next节点是否为空,接下来判定root的左右子节点是否为空就可以了。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root == null)
                return;
            TreeLinkNode node = root.next;
            
            if(node != null){
                if(node.right != null){
                    root.right.next = node.left;    
                    root.left.next = root.right;    
                }
            } else {
                if(root.right != null){
                    root.right.next = null;    
                    root.left.next = root.right;    
                }
           }
            
            connect(root.left);
            connect(root.right);
        }
    }

    Update:  

    之前写得好丑..精简一下。递归的Space Complexity怎么算? 这里建立了一个TreeLinkNode,是reference type,这个object的reference存在stack里,但object是存在heap里。  还要学习JVM的许多知识。

    Time Complexity - O(n), Space Complexity - O(1).

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root == null)
                return;
            TreeLinkNode p = root.next;
            if(root.left != null)
                root.left.next = root.right;
            if(root.right != null)
                root.right.next = (p == null) ? null : p.left;
            connect(root.left);
            connect(root.right);
        }
    }

    二刷:

    Java:

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null) {
                return;
            }
            TreeLinkNode p = root.next;
            if (root.left != null) {
                root.left.next = root.right;
                root.right.next = p == null ? null : p.left; 
            }
            connect(root.left);
            connect(root.right);
        }
    }

    三刷:

    跟之前的方法一样使用了recursive。但其实iterative的solution更好。

    Java:

    Recursive:

    Time Complexity - O(n), Space Complexity - O(n)

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null) return;
            if (root.right != null) {
                root.left.next = root.right;
                if (root.next != null) root.right.next = root.next.left;
            }
            connect(root.left);
            connect(root.right);
        }
    }

      

    Iterative:

    解法来自 yavinci大神。

    两层循环,当root和root.left不为空的时候,我们要利用好自己创建的next节点来不断向右进行遍历。遍历完这一层以后我们可以设置root = root.left,这样我们到了下一层,继续向右进行遍历。

    Time Complexity - O(n), Space Complexity - O(1)

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            while (root != null && root.left != null) {
                TreeLinkNode cur = root;
                while (cur != null) {
                    cur.left.next = cur.right;
                    if (cur.next != null) cur.right.next = cur.next.left;
                    cur = cur.next;
                }
                root = root.left;
            }
        }
    }

    Update:

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            TreeLinkNode curLevel = new TreeLinkNode(-1);
            TreeLinkNode newLevel = curLevel;
            while (root != null) {
                if (root.left != null) {
                    curLevel.next = root.left;
                    curLevel = curLevel.next;
                }
                if (root.right != null) {
                    curLevel.next = root.right;
                    curLevel = curLevel.next;
                }
                root = root.next;
                if (root == null) {
                    curLevel = newLevel;
                    root = newLevel.next;
                    newLevel.next = null;
                }
            }
        }
    }

    Reference:

    https://leetcode.com/discuss/7327/a-simple-accepted-solution

  • 相关阅读:
    互联网成为基础设施,数据成为生产资料,计算成为公共服务。让计算成为人类的能力,让数据成为世界的财富
    数据本没有价值,就像你留在地上的脚印,直到你有计算能力去处理才会产生价值。计算,如同其他基础设施一样,能够把沉寂的数据“沙子”变成经济、产业、财富的“金矿”。
    100多年前,铁路在美国成为基础设施,为美国带来翻天覆地的变化。今天互联网作为基础设施,对中国未来发展的推动绝对不会亚于当年铁路给美国带来的变化。
    HDU--1060
    素数
    欧几里德算法与扩展欧几里德算法
    HDU--1164
    挨踢人生路
    HDU题目分类
    HDU--2087
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437341.html
Copyright © 2020-2023  润新知