Prove safety offer0514: 1, the sequence of the binary tree; k-th node 2, the binary search tree; 3, median data stream

1, the sequence of binary
Title Description
Please implement two functions are used to serialize and deserialize binary

Note: not able to see the topic, read the comments area parsing know, more questions ah ~
1. serialization refers to the pre-order traversal through the binary tree becomes an array
2. deserialization means to rebuild binary tree
preorder traversal ago serialization, null sequence into a '#', index as a global variable
link: https://www.nowcoder.com/questionTerminal/cf7e25aa97c04cc1a68c8f040e71fb84
source: cattle-off network

  1. For the sequence of: preorder before use recursive binary value is converted to a character, a binary tree and each node
    is not empty, added after a conversion of the resulting val character ',' as a division. Places '#' in place for an empty node.
  2. For deserialized: according to the preamble sequence, recursive string of characters to create a binary tree (NB:
    When recursive, recursive function parameters must be a char **, so as to ensure to the string after each recursion pointer
    with recursive moves !!!)

Solution: recursive

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    flag = -1
    def Serialize(self, root):
        # write code here
        if not root:
            return '#'
        return str(root.val) + ',' + self.Serialize(root.left) + ',' + self.Serialize(root.right)
    def Deserialize(self, s):
        # write code here
        self.flag += 1
        l = s.split(',')
        if self.flag > len(s):
            return None
        
        root = None
        if l[self.flag] != '#':
            root = TreeNode(int(l[self.flag]))
            root.left = self.Deserialize(s)
            root.right = self.Deserialize(s)
        return root
        

2, binary search tree nodes of the k-th
topic description
given a binary search tree, find the k-th smaller nodes therein. For example, (5,3,7,2,4,6,8), the numerical values according to the third junction point is 4 summary.

Notes:
Special Note: returns the value of a binary tree node, the node is not! ! !
Binary search tree traversal sequence in order to print out the arranged sequence is descending order, so finding the k-th node is the result according inorder traversal order.

answer:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回对应节点TreeNode
    def KthNode(self, pRoot, k):
        # write code here
        global res
        res = []
        self.midorder(pRoot)
        if k==0 or len(res) < k:
            return None
        return res[k-1]
    def midorder(self,root):
        if not root:
            return None
        self.midorder(root.left)
        res.append(root)
        self.midorder(root.right)

Solution 2:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回对应节点TreeNode
    def KthNode(self, pRoot, k):
        if pRoot==None or k==0:
            return None
        n=self.isorder(pRoot)
        if len(n)<k:
            return None
        else:
            return n[k-1]
    def isorder(self,pRoot):
        re=[]
        if not pRoot:
            return None
        if pRoot.left:
            re.extend(self.isorder(pRoot.left))
        re.append(pRoot)
        if pRoot.right:
            re.extend(self.isorder(pRoot.right))
        return re

Median 3, the data stream

Title describes
how to get the median of a data stream? If the numerical value is read from the odd data stream, then the median value is located in the middle of all values after sorting. If an even number value is read out from the data stream, then the median is the average of two numbers after all intermediate values of order. We use the Insert () method to read the data stream, using GetMedian () method to get the current median data is read.

Notes: There in front of the subject, read only one character appears in the data stream, modeled on that, write their own code, the first major mistake is to prompt the number of variables GetMedian (), where attention to the code did not write, should be added on their own definition of variables

answer:

# -*- coding:utf-8 -*-
class Solution:
    def __init__(self):
        self.l = []
    def Insert(self, num):
        # write code here
        self.l.append(num)
        self.l.sort()
    def GetMedian(self,l):
        # write code here
        n = len(self.l)
        if n%2 == 1:
            return self.l[int((n-1)/2)]
        if n%2 == 0:
            return (self.l[n/2] + self.l[n/2-1])/2.0

Guess you like

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