Exemplo n.º 1
0
    def _deserialize(self, data, idxs):
        if len(data) == idxs[0]:
            return None

        val = data[idxs[0]]
        idxs[0] += 1
        if val == "#":
            return None
        else:
            node = TreeNode(int(val))
            node.left = self._deserialize(data, idxs)
            node.right = self._deserialize(data, idxs)
            return node
Exemplo n.º 2
0
	def invertTree(self, root: TreeNode) -> TreeNode:
		if root is None:
			return None

		root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)

		return root
# -*- coding: utf-8 -*-
__author__ = 'damon'

from common.utils import TreeNode
from typing import List


class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        ret = []
        self._postorderTraversal(root, ret)
        return ret

    def _postorderTraversal(self, root: TreeNode, ret: List[int]):
        if root is None:
            return
        self._postorderTraversal(root.left, ret)
        self._postorderTraversal(root.right, ret)
        ret.append(root.val)


if __name__ == '__main__':
    sl = Solution()
    a = TreeNode(1)
    b = TreeNode(2)
    c = TreeNode(3)
    a.right = b
    b.left = c

    print(sl.postorderTraversal(a))
def printFromTopToBottom(pNode):
    if pNode is None:
        return

    nodeQueue = deque()
    nodeQueue.append(pNode)

    while nodeQueue:
        pCur = nodeQueue.popleft()
        print pCur.value

        if pCur.left is not None:
            nodeQueue.append(pCur.left)

        if pCur.right is not None:
            nodeQueue.append(pCur.right)


if __name__ == "__main__":
    a1 = TreeNode(1)
    a2 = TreeNode(2)
    a3 = TreeNode(3)
    a4 = TreeNode(4)
    a5 = TreeNode(5)

    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5

    printFromTopToBottom(a1)
Exemplo n.º 5
0
def findPathCore(pRoot, expectedSum, path, curSum):
    curSum += pRoot.value
    path.append(pRoot.value)

    if pRoot.left is None and pRoot.right is None:
        if curSum == expectedSum:
            print path
    else:
        if pRoot.left is not None:
            findPathCore(pRoot.left, expectedSum, path, curSum)

        if pRoot.right is not None:
            findPathCore(pRoot.right, expectedSum, path, curSum)

    path.pop()


if __name__ == "__main__":
    a1 = TreeNode(10)
    a2 = TreeNode(5)
    a3 = TreeNode(12)
    a4 = TreeNode(4)
    a5 = TreeNode(7)

    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5

    findPath(a1, 22)
Exemplo n.º 6
0
    def _deserialize(self, data, idxs):
        if len(data) == idxs[0]:
            return None

        val = data[idxs[0]]
        idxs[0] += 1
        if val == "#":
            return None
        else:
            node = TreeNode(int(val))
            node.left = self._deserialize(data, idxs)
            node.right = self._deserialize(data, idxs)
            return node


if __name__ == '__main__':
    cd = Codec()

    a = TreeNode(-1)
    b = TreeNode(0)
    c = TreeNode(1)
    # d = TreeNode(4)
    # e = TreeNode(5)

    a.left = b
    a.right = c
    # c.left = d
    # c.right = e

    print(cd.serialize(a))
    print(cd.serialize(cd.deserialize(cd.serialize(a))))
    while pNewHead is not None and pNewHead.left is not None:
        pNewHead = pNewHead.left

    return pNewHead


def printList(pHead):
    values = []
    while pHead:
        values.append(pHead.value)
        pHead = pHead.right
    print values


if __name__ == "__main__":
    a1 = TreeNode(10)
    a2 = TreeNode(6)
    a3 = TreeNode(14)
    a4 = TreeNode(4)
    a5 = TreeNode(8)
    a6 = TreeNode(12)
    a7 = TreeNode(16)

    a1.left = a2
    a1.right = a3
    a2.left = a4
    a2.right = a5
    a3.left = a6
    a3.right = a7

    pHead = convert(a1)
    def createTree():
        a1 = TreeNode(1)
        a2 = TreeNode(2)
        a3 = TreeNode(3)
        a4 = TreeNode(4)
        a5 = TreeNode(5)

        a1.left = a2
        a1.right = a3
        a2.left = a4
        a2.right = a5
        return a1
Exemplo n.º 9
0
	return result


def isTree1HasTree2(pRoot1, pRoot2):
	if pRoot2 is None:
		return True
	if pRoot1 is None:
		return False
	if pRoot1.value != pRoot2.value:
		return False
	return isTree1HasTree2(pRoot1.left, pRoot2.left) and isTree1HasTree2(pRoot1.right, pRoot2.right)


if __name__ == "__main__":
	a1 = TreeNode(1)
	a2 = TreeNode(2)
	a3 = TreeNode(3)
	a4 = TreeNode(4)
	a5 = TreeNode(5)

	a1.left = a2
	a2.left = a3
	a3.left = a4
	a3.right = a5

	b1 = TreeNode(3)
	b2 = TreeNode(4)
	b3 = TreeNode(5)

	b1.left = b2