牛客-剑指offer第2版-Python编程(25,26题)

25、合并两个排序的链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

 

# -*- coding:utf-8 -*-
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        # write code here
        if pHead1 == None:
            return pHead2
        elif pHead2 == None:
            return pHead1

        pMergeHead = None
        if pHead1.val < pHead2.val:
            pMergeHead = pHead1
            pMergeHead.next = self.Merge(pHead1.next, pHead2)
        else:
            pMergeHead = pHead2
            pMergeHead.next = self.Merge(pHead1, pHead2.next)

        return pMergeHead

node1 = ListNode(1)
node2 = ListNode(3)
node3 = ListNode(5)
node1.next = node2
node2.next = node3

node4 = ListNode(2)
node5 = ListNode(4)
node6 = ListNode(6)
node4.next = node5
node5.next = node6

S = Solution()
S.Merge(node1, node4)
print(node3.next.val)

 

 

26、树的子结构

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

思路:

1、首先设置标志位result = false,因为一旦匹配成功result就设为true,
剩下的代码不会执行,如果匹配不成功,默认返回false
2、递归思想,如果根节点相同则递归调用DoesTree1HaveTree2(),
如果根节点不相同,则判断tree1的左子树和tree2是否相同,
再判断右子树和tree2是否相同
3、注意null的条件,HasSubTree中,如果两棵树都不为空才进行判断,
DoesTree1HasTree2中,如果Tree2为空,则说明第二棵树遍历完了,即匹配成功,
tree1为空有两种情况(1)如果tree1为空&&tree2不为空说明不匹配,
(2)如果tree1为空,tree2为空,说明匹配。
 

# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
# 递归终止条件:达到了树A或者树B的叶节点,即遍历完某树
class Solution:
    # 遍历函数
    def HasSubtree(self, pRoot1, pRoot2):
        # 设置标志位result,一旦匹配成功设为True,剩下的代码就不会执行
        # 如果匹配不成功,默认返回Flase
        result = False
        # 注意null的条件,HasSubTree中,如果两棵树都不为空才进行判断!!!
        if pRoot1 != None and pRoot2 != None:
            # 如果根节点值相同,递归调用函数
            # 查询是否具有相同的左、右子树结构
            if pRoot1.val == pRoot2.val:
                result = self.DoesTree1haveTree2(pRoot1, pRoot2)
            # 如果根节点不相同,则判断tree1的左子树和tree2是否相同,
            if not result:
                result = self.HasSubtree(pRoot1.left, pRoot2)
            # 再判断右子树和tree2是否相同
            if not result:
                result = self.HasSubtree(pRoot1.right, pRoot2)
        return result
    # 判断函数
    def DoesTree1haveTree2(self, pRoot1, pRoot2):
        # 如果Tree2为空,则说明第二棵树遍历完了,即匹配成功
        if pRoot2 == None: #此处判断,包含两个情况:pRoot1为空/不为空,pRoot2为空,均True
            return True
        # tree1为空有两种情况
        # (1)如果tree1为空&&tree2不为空说明不匹配,
        #(2)如果tree1为空,tree2为空,说明匹配。
        if pRoot1 == None:
            return False

        if pRoot1.val != pRoot2.val:
            return False
        # 如果值相同,则递归地判断它们各自的左右节点的值是不是相同
        return self.DoesTree1haveTree2(pRoot1.left, pRoot2.left) and self.DoesTree1haveTree2(pRoot1.right, pRoot2.right)


pRoot1 = TreeNode(8)
pRoot2 = TreeNode(8)
pRoot3 = TreeNode(7)
pRoot4 = TreeNode(9)
pRoot5 = TreeNode(2)
pRoot6 = TreeNode(4)
pRoot7 = TreeNode(7)
pRoot1.left = pRoot2
pRoot1.right = pRoot3
pRoot2.left = pRoot4
pRoot2.right = pRoot5
pRoot5.left = pRoot6
pRoot5.right = pRoot7

pRoot8 = TreeNode(8)
pRoot9 = TreeNode(9)
pRoot10 = TreeNode(2)
pRoot8.left = pRoot9
pRoot8.right = pRoot10

S = Solution()
print(S.HasSubtree(pRoot1, pRoot8))

 

你可能感兴趣的:(牛客)