Ejemplo n.º 1
0
        if pRoot==None:
            return False
        if (pRoot.left==None and pRoot.right==None):
            return True
        a=self.Pre(pRoot)
        b=self.Pre_duichen(pRoot)
        for i in range(len(a)):
            if (a[i]==None and b[i]!=None) or (a[i]!=None and b[i]==None) or (a[i]!=None and b[i]!=None and a[i].val!=b[i].val):
                return False
        return True

    def Pre(self,pHead):
        if pHead==None:
            return [None]
        return [pHead]+self.Pre(pHead.left)+self.Pre(pHead.right)

    def Pre_duichen(self,pHead):
        if pHead==None:
            return [None]
        return [pHead]+self.Pre_duichen(pHead.right)+self.Pre_duichen(pHead.left)

#--------------------------------------

a=Solution()
# ll=[1,2,3]
# ll.reverse()
# pRoot=create_tree.fromList([1,2,3,4,5,6,7])
pRoot=create_tree.fromList([8,6,6,5,7,7,5])
# pRoot=create_tree.fromList([5,5,5,5,None,None,5,5,None,5])

print(a.isSymmetrical(pRoot))
Ejemplo n.º 2
0
import create_tree


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def Mirror(self, root):
        # write code herero
        node_list = [root]
        for start in node_list:
            while start != None:
                tmp = start.left
                start.left = start.right
                start.right = tmp
                # node_list.remove(start)
                node_list.extend([start.left, start.right])
                break
        return root


a = Solution()
root = create_tree.fromList([8, 6, 10, 5, 7, 9, 11])
b = a.Mirror(root)
print(b)
Ejemplo n.º 3
0
import create_tree


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def TreeDepth(self, pRoot):
        if pRoot == None:
            return 0
        if pRoot.left == None and pRoot.right == None:
            return 1
        return max(1 + self.TreeDepth(pRoot.left),
                   1 + self.TreeDepth(pRoot.right))


a = Solution()
b = create_tree.fromList([1, 2, 3, 4, 5, 6, 7, 8])
c = a.TreeDepth(b)
print(c)
Ejemplo n.º 4
0
#             return True
#         if pRoot1.val==pRoot2.val:
#             return self.IsSubtree(pRoot1.left,pRoot2.left) and self.IsSubtree(pRoot1.right,pRoot2.right)
#         else:
#             return False


#----niuke 能通过
class Solution:
    def HasSubtree(self, pRoot1, pRoot2):
        # write code here
        if not pRoot1 or not pRoot2:
            return False
        return self.HasSubtree(pRoot1.left, pRoot2) or self.HasSubtree(
            pRoot1.right, pRoot2) or self.is_subtree(pRoot1, pRoot2)

    def is_subtree(self, A, B):
        if not B:
            return True
        if not A or A.val != B.val:
            return False
        return self.is_subtree(A.left, B.left) and self.is_subtree(
            A.right, B.right)


a = Solution()
A = create_tree.fromList([8, None, 8, None, 9, None, 2, None, 5])
B = create_tree.fromList([8, None, 9, 3, 2])
c = a.HasSubtree(A, B)
print(c)
Ejemplo n.º 5
0
import create_tree

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def IsBalanced_Solution(self, pRoot):
        return self.subBalance(pRoot)[1]
    def subBalance(self,pRoot):
        if pRoot==None:
            return 0,True

        left_len,left_flag=self.subBalance(pRoot.left)
        right_len,right_flag=self.subBalance(pRoot.right)
        if abs(left_len-right_len)<=1:
            return max(left_len+1,right_len+1), left_flag and right_flag
        else:
            return max(left_len+1,right_len+1), False




a=Solution()
b=create_tree.fromList([5,4,None,3,None])
c=a.IsBalanced_Solution(b)
print(c)
Ejemplo n.º 6
0
    #     #else:
    #      #   return None
    # def getList(self,pRoot):
    #     if pRoot.left==None and pRoot.right==None:
    #         self.nodeList.append(pRoot)
    #         # return self.nodeList
    #         return None
    #     self.getList(pRoot.left)
    #     self.nodeList.append(pRoot)
    #     self.getList(pRoot.right)
    #     # return self.nodeList

    def KthNode(self, pRoot, k):
        l_list = self.Getlist(pRoot)
        if k >= len(l_list):
            return None
        return l_list[k]

    def Getlist(self, pRoot):
        if pRoot == None:
            return []
        if pRoot.left == None and pRoot.right == None:
            return [pRoot]
        return self.Getlist(pRoot.left) + [pRoot] + self.Getlist(pRoot.right)


a = Solution()
pRoot = create_tree.fromList([8, 6, 10, 5, None, 9, 11])
k = 8
c = a.KthNode(pRoot, k)
print(c)
Ejemplo n.º 7
0
        if pRootOfTree.left == None and pRootOfTree.right == None:
            return pRootOfTree
        if pRootOfTree.left != None:
            self.Convert(pRootOfTree.left)
            a = pRootOfTree.left
            while a.right != None:
                a = a.right
            pRootOfTree.left = a
            a.right = pRootOfTree

        if pRootOfTree.right != None:
            self.Convert(pRootOfTree.right)
            b = pRootOfTree.right
            while b.left != None:
                b = b.left
            pRootOfTree.right = b
            b.left = pRootOfTree
        while pRootOfTree.left != None:
            pRootOfTree = pRootOfTree.left
        return pRootOfTree


a = Solution()
# pRootOfTree=create_tree.fromList([5,4,None,3,None,None,None,2,None,1,None])
pRootOfTree = create_tree.fromList([
    5, 4, None, 3, None, None, None, 2, None, None, None, None, None, None,
    None, 1
])
c = a.Convert(pRootOfTree)
print(c)
Ejemplo n.º 8
0
                return True
        else:
            if pRoot1.left != None:
                result_left = self.HasSubtree(pRoot1.left, pRoot2)
                if result_left == True:
                    return True
                else:
                    if pRoot1.right != None:
                        result_right = self.HasSubtree(pRoot1.right, pRoot2)
                        if result_right == True:
                            return True
                        else:
                            return False
                    else:
                        return False
            else:
                if pRoot1.right != None:
                    result_right = self.HasSubtree(pRoot1.right, pRoot2)
                    if result_right == True:
                        return True
                    else:
                        return False
                else:
                    return False


a = Solution()
pRoot1 = create_tree.fromList([8, 8, 7, 9, 2, None, None, None, None, 4, 7])
pRoot2 = create_tree.fromList([8, 9, 2])

print(a.HasSubtree(pRoot1, pRoot2))
Ejemplo n.º 9
0
    #         i=i.insert(0,start.val)
    #     return leftpath+rightpath






    def FindPath(self, root, expectNumber):
        if root.left==None and root.right==None:
            if root.val==expectNumber:
                return [[root.val]]
            else:
                return []

        if root.left!=None:
            a=self.FindPath(root.left,expectNumber-root.val)

        if root.right!=None:
            b=self.FindPath(root.right,expectNumber-root.val)
        for i in a+b:
            i.insert(0,root.val)

        return a+b



a=Solution()
root=create_tree.fromList([10,5,12,4,7])
b=a.FindPath(root,22)
print(b)