zoukankan      html  css  js  c++  java
  • 117. Populating Next Right Pointers in Each Node II

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/#/description

    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
    
     
     
    Sol 1:
     
    Recursion.
     
    # Definition for binary tree with next pointer.
    # class TreeLinkNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    #         self.next = None
    
    class Solution:
        # @param root, a tree link node
        # @return nothing
        def connect(self, root):
            
            
        # recursion
        # it can solve Populating Next Right Pointers in Each Node I.
        # To handle the right sub node, the first thought pops out is BFS.  However, BFS does not take up constant space
        # Time O(n) Space(1)
        
            if not root:
                return
            
            # dummy stores node on the previous level. 
            dummy = TreeLinkNode(0)
            curr = root
            prev = dummy
            while curr:
    
                if curr.left:
                    prev.next = curr.left
                    prev = prev.next
                if curr.right:
                    prev.next = curr.right
                    prev = prev.next   
                curr = curr.next
            
            self.connect(dummy.next)
        
     

     Note:

    1 Use built in class given by the question. 

    Get the first element of a tree using class TreeLinkNode.

    dummy = TreeLinkNode(0)

    Sol 2:

    Iteration. 

    Not very different from the recursion method. Just change the way we forward the dummy variable.

    "

     The algorithm is a BFS or level order traversal. We go through the tree level by level. node is the pointer in the parent level, tail is the tail pointer in the child level.
    The parent level can be view as a singly linked list or queue, which we can traversal easily with a pointer.
    Connect the tail with every one of the possible nodes in child level, update it only if the connected node is not nil.
    Do this one level by one level. The whole thing is quite straightforward.

    "

    # Definition for binary tree with next pointer.
    # class TreeLinkNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    #         self.next = None
    
    class Solution:
        # @param root, a tree link node
        # @return nothing
        
        
        def connect(self, node):
    
        # iteration
            tail = dummy = TreeLinkNode(0)
            while node:
                tail.next = node.left
                if tail.next:
                    tail = tail.next
                tail.next = node.right
                if tail.next:
                    tail = tail.next
                node = node.next
                if not node:
                    tail = dummy
                    node = dummy.next
                    
        
  • 相关阅读:
    hadoop(四)HDFS的核心设计
    spark(三)spark sql
    hadoop(三)HDFS基础使用
    hadoop(二)hadoop集群的搭建
    IOS微信中看文章跳转页面后点击返回无效
    使用GBK编码请求访问nodejs程序报415错误:Error: unsupported charset at urlencodedParser ...
    mac端口占用查找进程并杀死
    tinymce4.x 上传本地图片(自己写个插件)
    mongodb使用mongoose分组查询
    javascript/jquery给动态加载的元素添加click事件
  • 原文地址:https://www.cnblogs.com/prmlab/p/7218656.html
Copyright © 2011-2022 走看看