Prove safety offer0509: 1, the list entry ring node / 2, delete duplicate node list; next node binary tree

Central node list entry

Description Title
to a linked list, wherein if the ring comprises, find the entry ring node list, otherwise, outputs null.
Note: The list really not familiar with, is actually equivalent to find a list of repeated elements

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        if not pHead:
            return pHead
        d = []
        p = pHead
        while p:
            if p in d:
                return p
            else:
                d.append(p)
            p = p.next

/2

Delete duplicate node in the list

Description Title
in a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5

Notes: Note that returns a list, and the pointer value containing

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def deleteDuplication(self, pHead):
        # write code here
        if not pHead:
            return pHead
        d = []
        while pHead:
            d.append(pHead.val)
            pHead = pHead.next
        p = q = ListNode(0)
        for i in d:
            if d.count(i)==1:
                q.next = ListNode(i)
                q = q.next
        return p.next

The next section describes binary tree topic

And wherein a given binary tree of a node, find the next node in a preorder traversal order and returns. Note that the node in the tree contains not only the left and right child nodes, the parent node contains a pointer pointing to.

Notes: After reading resolve came to understand, repeatedly review the proficiency

Ideas: First, know that in order traversal rule is: the root of the left and right, and then plotted
Here Insert Picture Descriptionin conjunction with map, we can find divided into two categories: 1, has the right subtree, then the next node is the left-most point the right subtree ; (eg: D, B, E, a, C, G) 2, there is no right subtree, may be divided into two groups, a) is the left child node of the parent (eg: N, I, L ), then the parent node is the next node; b) is the right child of the parent node (eg: H, J, K , M) looking for his parent parent parent node ... until the current node is the left child of its parent location. If there is no eg: M, he is the tail node.

Next node binary tree analysis, a total of the following cases:
1. The binary tree is empty, return null;
2. right child nodes exist, a pointer is provided from the right child node, the child nodes along the way to the left refers to leaf node is the pointer to find the next node;
3. the node is not the root node. If the node is left child of its parent node, the parent node is returned; otherwise, continue to traverse up the parent node of its parent node, before the judge repeated, returns the result. code show as below:

# -*- coding:utf-8 -*-
# class TreeLinkNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#         self.next = None
class Solution:
    def GetNext(self, pNode):
        # write code here
        if not pNode:
            return pNode
        if pNode.right:
            left1 = pNode.right
            while left1.left:
                left1 = left1.left
            return left1
        while pNode.next:
            tmp = pNode.next
            if tmp.left == pNode:
                return tmp
            pNode = tmp

Guess you like

Origin blog.csdn.net/Leia21/article/details/90053083