题目来源:
自我感觉难度/真实难度:
题意:
分析:
自己的代码:
class Solution(object):def findTarget(self, root, k):""":type root: TreeNode:type k: int:rtype: bool"""All=self.InOrder(root)Plus=[]#for i in range(All.size()):for i in range(len(All)):for j in range(len(All)):Plus.append(All[i]+All[j])if k in Plus:return Truereturn Falsedef InOrder(self,root):if not root:returnres=[]

res.append(root.val)
self.InOrder(root.left) self.InOrder(root.right) return res 

1.中序遍历不会,这不是中序遍历

2.这样没有得到中序遍历

代码效率/结果:
优秀代码:
# 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 findTarget(self, root, k):""":type root: TreeNode:type k: int:rtype: bool"""res = self.inOrder(root)resset = set(res)for num in res:if k != 2 * num and k - num in resset:return Truereturn Falsedef inOrder(self, root):if not root:return []res = []res.extend(self.inOrder(root.left))res.append(root.val)res.extend(self.inOrder(root.right))return res

代码效率/结果:

Runtime: 84 ms, faster than 56.86% of Python online submissions for Two Sum IV - Input is a BST.

优化后的代码:
class Solution(object):def findTarget(self, root, k):""":type root: TreeNode:type k: int:rtype: bool"""bfs, s = [], set()bfs = [root]for i in bfs:if k - i.val in s:return Trues.add(i.val)if i.left:bfs.append(i.left)if i.right:bfs.append(i.right)return False

Runtime: 108 ms, faster than 33.56% of Python online submissions for Two Sum IV - Input is a BST.

这个时间测量是不是有问题呢?

反思改进策略:

   1.使重复元素只有一个的办法:set()

     2.中序的过程中,一定要res.extend,不然没保存到内容

转载于:https://www.cnblogs.com/captain-dl/p/10187712.html

653. Two Sum IV - Input is a BST相关推荐

  1. LeetCode 653. Two Sum IV - Input is a BST

    题目: Given a Binary Search Tree and a target number, return true if there exist two elements in the B ...

  2. LeetCode 653. Two Sum IV - Input is a BST--Python解法

    题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tree and a target number, return t ...

  3. C#LeetCode刷题之#653-两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4098 访问. 给定一个二叉搜索树和一个目标结果,如果 BST 中 ...

  4. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

  5. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  6. LeetCode 167. Two Sum II - Input array is sorted

    题目 : Given an array of integers that is already sorted in ascending order, find two numbers such tha ...

  7. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

  8. 167. Two Sum II - Input array is sorted (C, C++, Python)

    本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++. 问题: Given an array of integers that is already sorted ...

  9. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

最新文章

  1. consul使用的一些见解
  2. ubuntu 下 安装 sublime Text3
  3. 12星座的出生年月日性格_星座六点半/今日12月04日:双鱼、巨蟹、天蝎座运势...
  4. android adb命令使用
  5. Linux报错问题:bash: vi: command not found
  6. 正确调用腾讯x5内核详解
  7. 软考软件设计师考试总结(2018上半年)
  8. matlab用平方法怎么求传递闭包矩阵,逐次平方法计算传递闭包
  9. 团队作业九---项目验收与总结
  10. Android Logcat输出为何能自动换行输出的原因以及多\n的作用
  11. Flappy bird制作过程
  12. windows删除文件夹时提示:你需要权限来执行此操作
  13. 通用验证码识别SDK免费开源
  14. 三星S6D1121主控彩屏(240*320*18bit,262K)驱动程序
  15. 20190603复盘
  16. Mac下嵌入式开发问题初步
  17. 【行空板教程】写字板
  18. tcp/ip通讯 linux xpe,XPE最基本组件 分享
  19. PTA 数组 7-2 逆序存放数组中的数据,并输出指定元素
  20. 生成对抗网络(GAN)研究年度进展评述 2017

热门文章

  1. android 传感器ceshi,Android代码-传感器-测试手机支持那几种传感
  2. python选择表单_如何使用Python在表单中选择选项?
  3. 【NC54 三数之和】(待整理)
  4. 重读经典:《ImageNet Classification with Deep Convolutional Neural Networks》
  5. 吴恩达机器学习作业(1):线性回归
  6. Angular中父子组件传值@Input @Output @ViewChild最全面最简单的总结
  7. html div分钟刷新一次啊代码_接口测试平台代码实现57首页重构5
  8. 大一计算机绩点3算什么水平,绩点只有3?我可以解释一下
  9. c++查询当前文件夹下文件数目_python3自动化小工具--删除某个文件夹xx后缀文件...
  10. 编写程序,随机产生20个0到1之间的数,将这20个数写入文本文件中,要求每行5个数