LC Tree

简单

  1. 相同树
#Method1
def isSameTree(self,p,q):
	if not p and not q:
		return True
	elif not p or not q:
		return False
	else:
		return p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
  1. 对称树
def MirrorTree(self,root):
	#Method1 recursion
	if not root:
		return True
	def isMirror(root1,root2):
		if not root1 and not root2:
			return True
		if root1 and root2 and root1.val==root2.val:
			return isMirror(root1.left,root2.right) and isMirror(root1.right,root2.left)
		return False
	return isMirror(root.left,root.right)
	#Method2 iteration
	stack = [root]
        while stack:
            nodes,ans = [],[]
            for x in stack:
                if not x: 
                    ans.append(None)
                    continue
                ans.append(x.val)
                nodes.append(x.left)
                nodes.append(x.right)
            stack = nodes
            if ans!=ans[::-1]:
                return False
        return True
  1. 二叉树的最大深度
def maxDepth(self,root):
	if not root:
		return 0
	return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))
  1. 二叉树的层序遍历 II
#Method1 iteration
def levelOrderBottom(self, root):
	#Method1 iteration
    stack,res = [root],[]
    while stack:
        nodes,ans= [],[]
        for x in stack:
            if not x: continue
            ans.append(x.val)
            nodes.append(x.left)
            nodes.append(x.right)
        stack = nodes
        res.append(ans)
    return res[::-1][1:]
    #Method2 recursion
    if not root: return []
        res = []
        def dfs(root,depth):
            if not root: return
            if depth == len(res):
                res.append([])
            res[depth].append(root.val)
            dfs(root.left,depth+1)
            dfs(root.right,depth+1)
        dfs(root,0)
        return res[::-1]
  1. 平衡二叉树
def isBalanced(self, root):
	if not root: return True
    return abs(self.depth(root.left)-self.depth(root.right))<=1 and self.isBalanced(root.left) and self.isBalanced(root.right)

def depth(self,root):
	if not root:
		return 0
    return 1+max(self.depth(root.left),self.depth(root.right))
  1. 二叉树最小深度:
def minDepth(self, root):
	if not root: return 0
    if root.left and root.right:
    	return min(self.minDepth(root.left),self.minDepth(root.right))+1
    elif root.left:
        return 1+self.minDepth(root.left)
    elif root.right:
        return 1+self.minDepth(root.right)
    else:
        return 1
  1. 路径总和
def hasPathSum(self, root, sum_):
    res = []
    def dfs(root,tmp):
        if not root: return
        if not root.left and not root.right and sum(tmp)+root.val==sum_:
            res.append(1)
            return 
        dfs(root.left,tmp+[root.val])
        dfs(root.right,tmp+[root.val])
    dfs(root,[])
    return len(res)>0
  1. 二叉搜索树的最近公共祖先
def lowestCommonAncestor(self, root, p, q):
	if p.val<root.val and q.val<root.val:
		return self.lowestCommonAncestor(root.left,p,q)
	if p.val > root.val and q.val>root.val:
		return self.lowestCommonAncestor(root.right,p,q)
    return root
  1. 二叉树的所有路径
def binaryTreePaths(self, root):
    res = []
    def dfs(root,tmp):
        if not root: return 
        if not root.left and not root.right:
            res.append(tmp+[str(root.val)])
        dfs(root.left,tmp+[str(root.val)])
        dfs(root.right,tmp+[str(root.val)])
    dfs(root,[])
    return ['->'.join(x) for x in res]
  1. 二叉树的直径
def diameterOfBinaryTree(self, root):
    self.res = 0
    def dfs(root):
        if not root: return 0
        l,r =dfs(root.left),dfs(root.right)
        self.res = max(self.res,l+r)
        return max(l,r)+1
    dfs(root)
    return self.res
  1. 另一个树的子树
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
	if not s and not t: return True
    if not s and t or not t and s: return False
    return self.isSametree(s,t) or self.isSubtree(s.left,t) or self.isSubtree(s.right,t)
    
def isSametree(self,x,y):
    if not x and not y:
        return True
    if (not x and y) or (x and not y):
        return False
    else:
        return x.val==y.val and self.isSametree(x.left,y.left) and self.isSametree(x.right,y.right) 
  1. 修剪二叉搜索树
def trimBST(self,root,L,R):
    if not root:
        return None
    if root.val<L:
        return self.trimBST(root.right,L,R)
    if root.val>R:
        return self.trimBST(root.left,L,R)
    root.left = self.trimBST(root.left,L,root.val)
    root.right = self.trimBST(root.right,root.val,R)
    return root
  1. N叉树的前序遍历
def preorder(self,root):
    #Method1 recursion
    if not root: return []
    res = [root.val]
    for child in root.children:
        res += self.preorder(child)
    return res
    #Method2 iteration
    if not root: return []
    stack,res = [root],[]
    while stack:
        node = stack.pop()
        res.append(node.val)
        stack += node.children[::-1]
    return res

中等

困难

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值