Leetcode Tree 知识点总结

136 篇文章 0 订阅
33 篇文章 0 订阅

思路:先确定root,在递归获取root.left和root.right
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def generateTrees(self, n):
        """
        :type n: int
        :rtype: List[TreeNode]
        """
        arrays = [i for i in range(1,n+1)]
        return self.dp(arrays)

    def dp(self,arrays):
        if len(arrays) == 1:
            return [TreeNode(arrays[0])]

        ret = []
        for k in range(len(arrays)):
            num = arrays[k]
            l = arrays[0:k]
            r = arrays[k+1:]
            left = self.dp(l)
            right = self.dp(r)

            if left == []:
                for j in range(len(right)):
                    Node = TreeNode(num)
                    Node.right = right[j]
                    ret.append(Node)

            if right == []:
                for i in range(len(left)):
                    Node = TreeNode(num)
                    Node.left = left[i]
                    ret.append(Node)

            for i in range(len(left)):
                for j in range(len(right)):
                    Node = TreeNode(num)
                    Node.left = left[i]
                    Node.right = right[j]
                    ret.append(Node)

        return ret

二叉搜索树的性质是中序遍历结果为有序数组,利用这个性质先获取原树中序遍历的结果,然后寻找最高波峰和最低低谷,再用dfs来修正原来的树即可
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import sys
class Solution(object):
    def recoverTree(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        stack = [root]
        arrays = [-sys.maxint]
        while(stack):
            item = stack.pop()
            if item.left == None and item.right == None:
                arrays.append(item.val)
            else:
                if item.right:
                    stack.append(item.right)
                stack.append(TreeNode(item.val))
                if item.left:
                    stack.append(item.left)

        arrays.append(sys.maxint)
        val1,val2 = -sys.maxint,sys.maxint

        for i in range(1,len(arrays)-1):
            if arrays[i]>arrays[i-1] and arrays[i]>arrays[i+1] and arrays[i]>val1:
                val1 = arrays[i]
            if arrays[i]<arrays[i-1] and arrays[i]<arrays[i+1] and arrays[i]<val2:
                val2 = arrays[i]

        def dfs(root,val1,val2):
            if root.val == val1:
                root.val = val2
            elif root.val == val2:
                root.val = val1
            if root.left:
                dfs(root.left,val1,val2)
            if root.right:
                dfs(root.right,val1,val2)
        dfs(root,val1,val2)

设置最大最小边界进行递归判断
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import sys
class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        min_ = -sys.maxint
        max_ = sys.maxint
        return self.checkBST(root,min_,max_)

    def checkBST(self,root,min_,max_):
        if root is None: return True
        if root.val <= min_ or root.val >= max_:
            return False
        return self.checkBST(root.left,min_,root.val) and self.checkBST(root.right,root.val,max_)


  • 814. Binary Tree Pruning:给树剪枝,如果该节点的值和其子孙节点的值均为0,则溢出该节点,Medium

计算节点的值为该节点的值+所有子孙节点值的和

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def pruneTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if self.nodeValues(root) == 0: return None
        root.left = self.pruneTree(root.left)
        root.right = self.pruneTree(root.right)
        return root

    def nodeValues(self,root):
        if root is None: return 0
        return root.val+self.nodeValues(root.left)+self.nodeValues(root.right)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值