二叉树专题

7. 二叉树的序列化和反序列化

设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。

如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。

样例
样例 1:

输入:{3,9,20,#,#,15,7}
输出:{3,9,20,#,#,15,7}
解释:
二叉树 {3,9,20,#,#,15,7},表示如下的树结构:
3
/
9 20
/
15 7
它将被序列化为 {3,9,20,#,#,15,7}
样例 2:

输入:{1,2,3}
输出:{1,2,3}
解释:
二叉树 {1,2,3},表示如下的树结构:
1
/
2 3
它将被序列化为 {1,2,3}

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param root: An object of TreeNode, denote the root of the binary tree.
    This method will be invoked first, you should design your own algorithm 
    to serialize a binary tree which denote by a root node to a string which
    can be easily deserialized by your own "deserialize" method later.
    """
    def serialize(self, root):
        # write your code here
        if not root:
            return ['#']
        ans = []
        ans.append(str(root.val))
        ans += self.serialize(root.left)
        ans += self.serialize(root.right)
        return ans
    
    
    def deserialize(self, data):
        # write your code here
        ch = data.pop(0)
        if ch == '#':
            return None
        else:
            root = TreeNode(int(ch))
        root.left = self.deserialize(data)
        root.right = self.deserialize(data)
        return root

175. 翻转二叉树

翻转一棵二叉树。左右子树交换。

样例
样例 1:

输入: {1,3,#}
输出: {1,#,3}
解释:
1 1
/ =>
3 3
样例 2:

输入: {1,2,3,#,#,4}
输出: {1,3,2,#,4}
解释:

  1         1
 / \       / \
2   3  => 3   2
   /       \
  4         4
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: a TreeNode, the root of the binary tree
    @return: nothing
    """
    def invertBinaryTree(self, root):
        # write your code here
        def infer(root):
            if root == None:
                return
            temp = root.right
            root.right = root.left
            root.left = temp
            
            infer(root.left)
            infer(root.right)
            
        infer(root)

95. 验证二叉查找树

给定一个二叉树,判断它是否是合法的二叉查找树(BST)

一棵BST定义为:

节点的左子树中的值要严格小于该节点的值。
节点的右子树中的值要严格大于该节点的值。
左右子树也必须是二叉查找树。
一个节点的树也是二叉查找树。
样例
样例 1:

输入:{-1}
输出:true
解释:
二叉树如下(仅有一个节点):
-1
这是二叉查找树。
样例 2:

输入:{2,1,4,#,#,3,5}
输出:true
解释:
二叉树如下:
2
/
1 4
/
3 5
这是二叉查找树

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: True if the binary tree is BST, or false
    """
    def isValidBST(self, root):
        # write your code here
        self.root_val = None
        
        self.result =True
        
        self.infer(root)
        print(self.result)
        return self.result
        
    def infer(self,root):
        if root == None:
            return
        self.infer(root.left)
        if self.root_val is not None and self.root_val >= root.val:
            self.result = False
            return
        self.root_val = root.val
        self.infer(root.right)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值