天天看点

7.2笔记

669.修剪二叉搜索树(递归):

class Solution:    
	def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode:        
		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,R)        
		root.right = self.trimBST(root.right,L,R)        
	return root
           

671.二叉树中第二小的节点:

class Solution:    
	def findSecondMinimumValue(self, root: TreeNode) -> int:        
		a = root.val         
		stack = [root]        
		res = []        
		while stack:        
			stack1 = []            
			for i in stack:                
				if a != i.val:                    
					res.append(i.val)                
				if i.left:                    
					stack1.append(i.left)                
				if i.right:                    
					stack1.append(i.right)            
			stack = stack1        
		return min(res) if res else -1