树的操作

树的操作(Python)

树的前序、中序、后序遍历

def preOrder(root):
    if not root:
        return []
    res = []
    nodes = [root]
    while nodes:
        node = nodes.pop()
        res.append(node.val)
        if node.right:
            nodes.append(node.right)
        if node.left:
            nodes.append(node.left)
    return res

def midOrder(root):
    if not root:
        return []
    nodes = []
    res = []
    node = root
    while node or nodes:
        if node:
            nodes.append(node)
            node = node.left
        else:
            node = nodes.pop()
            res.append(node.val)
            node = node.right
    return res

def postOrder(root):
    if not root:
        return []
    res = []
    nodes1 = [root]
    nodes2 = []
    while nodes1:
        node = nodes1.pop()
        nodes2.append(node)
        if node.left:
            nodes1.append(node.left)
        if node.right:
            nodes1.append(node.right)
    while nodes2:
        res.append(node2.pop().val)
    return res

树的层序遍历

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回二维列表[[1,2],[4,5]]
    def Print(self, pRoot):
        # write code here
        if not pRoot:
            return []
        nodes = [pRoot]
        res = []
        while len(nodes) != 0:
            curNodes = []
            ans= []
            for node in nodes:
                ans.append(node.val)
                if node.left:
                    curNodes.append(node.left)
                if node.right:
                    curNodes.append(node.right)
            nodes = curNodes
            res.append(ans)
        return res

树的深度

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        if not pRoot:
            return 0
        nodes = [pRoot]
        depth = 0
        while nodes:
            curNodes = []
            for node in nodes:
                if node.left:
                    curNodes.append(node.left)
                if node.right:
                    curNodes.append(node.right)
            nodes = curNodes
            depth += 1
        return depth

对称树

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def isSymmetrical(self, pRoot):
        # write code here
        if not pRoot:
            return True
        nodes = [pRoot.left, pRoot.right]
        while nodes:
            leftnode = nodes.pop(0)
            rightnode = nodes.pop(0)
            if leftnode and rightnode:
                if leftnode.val == rightnode.val:
                    nodes.append(leftnode.left)
                    nodes.append(rightnode.right)
                    nodes.append(leftnode.right)
                    nodes.append(rightnode.left)
                    continue
                return False
            if not leftnode and not rightnode:
                continue
            return False
        return True

平衡树

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        # write code here
        if not pRoot:
            return True
        if not self.IsBalanced_Solution(pRoot.left):
            return False
        if not self.IsBalanced_Solution(pRoot.right):
            return False
        if abs(self.getDepth(pRoot.left) - self.getDepth(pRoot.right)) > 1:
            return False
        return True

    def getDepth(self, Root):
        if not Root:
            return 0
        return max(self.getDepth(Root.left), self.getDepth(Root.right)) + 1

镜像树

# 镜像树
# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回镜像树的根节点
    def Mirror(self, root):
        # write code here
        if not root:
            return None
        nodes = [root]
        while nodes:
            node = nodes.pop(0)
            tmp = node.left
            node.left = node.right
            node.right = tmp

            if node.left:
                nodes.append(node.left)
            if node.right:
                nodes.append(node.right)
        return root

你可能感兴趣的:(算法,Python)