当前位置:   article > 正文

代码随想录-Day20_代码随想录 day20

代码随想录 day20
  1. # lc654 最大二叉树
  2. #Definition for a binary tree node.
  3. # class TreeNode:
  4. # def __init__(self, val=0, left=None, right=None):
  5. # self.val = val
  6. # self.left = left
  7. # self.right = right
  8. class Solution:
  9. def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:#【二轮】可以学习迭代法
  10. if not nums:
  11. return
  12. node_val=max(nums)
  13. root=TreeNode(node_val)
  14. node_idx=nums.index(node_val)
  15. node_left=nums[:node_idx]
  16. node_right=nums[node_idx+1:]
  17. root.left=self.constructMaximumBinaryTree(node_left)
  18. root.right=self.constructMaximumBinaryTree(node_right)
  19. return root
  1. #lc617 合并二叉树
  2. class Solution:
  3. def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
  4. #【二轮】掌握迭代法
  5. if not root1 and not root2:
  6. return
  7. if not root1: return root2####【二轮】看一遍,这是答案代码,我的代码是: if not root1: root1=TreeNode(0)
  8. if not root2: return root1####【二轮】看一遍,这是答案代码,我的代码是: if not root2: root2=TreeNode(0)
  9. root=TreeNode(root1.val+root2.val)
  10. root.left=self.mergeTrees(root1.left, root2.left)
  11. root.right=self.mergeTrees(root1.right, root2.right)
  12. return root
  1. #700 二叉搜索树中的搜索
  2. class Solution:
  3. def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:#迭代法
  4. #二叉搜索树是一个有序树:(左小右大)
  5. # 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
  6. # 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
  7. # 它的左、右子树也分别为二叉搜索树
  8. # if not root:
  9. # return
  10. while root:
  11. #print(root)
  12. if root.val==val:
  13. return root
  14. elif root.val>val:
  15. root=root.left####此处是root.left 而非root.right
  16. elif root.val<val:####反之亦然
  17. root=root.right
  18. return
  1. #lc98 验证二叉搜索树
  2. class Solution:
  3. def isValidBST(self, root: Optional[TreeNode]) -> bool:
  4. #递归+数组升序判断解法,此方法较繁琐【二轮】学会迭代+双指针解法
  5. result=[]
  6. if not root:
  7. return False
  8. def Traversal(node):
  9. if not node:
  10. return
  11. Traversal(node.left)
  12. result.append(node.val)
  13. Traversal(node.right)
  14. Traversal(root)
  15. for i in range(len(result)-1):
  16. if result[i]>=result[i+1]:
  17. return False
  18. return True
  19. 迭代-中序遍历
  20. class Solution:
  21. def isValidBST(self, root: TreeNode) -> bool:
  22. stack = []
  23. cur = root
  24. pre = None
  25. while cur or stack:
  26. if cur: # 指针来访问节点,访问到最底层
  27. stack.append(cur)
  28. cur = cur.left
  29. else: # 逐一处理节点
  30. cur = stack.pop()
  31. if pre and cur.val <= pre.val: # 比较当前节点和前节点的值的大小
  32. return False
  33. pre = cur
  34. cur = cur.right
  35. return True
  36. # 遵循Carl的写法,只添加了节点判断的部分
  37. class Solution:
  38. def isValidBST(self, root: TreeNode) -> bool:
  39. # method 2
  40. que, pre = [], None
  41. while root or que:
  42. while root:
  43. que.append(root)
  44. root = root.left
  45. root = que.pop()
  46. # 对第一个节点只做记录,对后面的节点进行比较
  47. if pre is None:
  48. pre = root.val
  49. else:
  50. if pre >= root.val: return False
  51. pre = root.val
  52. root = root.right
  53. return True

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/555910
推荐阅读
相关标签
  

闽ICP备14008679号