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 BST such that their sum is equal to the given target.
题目分析及思路
给定一个二叉搜索树和一个目标值,若该树中存在两个元素的和等于目标值则返回true。可以先获得树中所有结点的值列表,然后遍历每个值,判断目标值与该值的差是否也在该列表中,并保存每个值的判断结果。若有一个true则返回true。
python代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findTarget(self, root: TreeNode, k: int) -> bool:
vals = []
q = collections.deque()
q.append(root)
while q:
size = len(q)
for _ in range(size):
node = q.popleft()
if not node:
continue
vals.append(node.val)
q.append(node.left)
q.append(node.right)
ans = []
for v in vals:
if k-v in vals and k-v != v:
ans.append(True)
else:
ans.append(False)
if True in ans:
return True
else:
return False
LeetCode 653 Two Sum IV - Input is a BST 解题报告的更多相关文章
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- [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 BST s ...
- 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 BST s ...
- 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 BST s ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 【Leetcode_easy】653. Two Sum IV - Input is a BST
problem 653. Two Sum IV - Input is a BST 参考 1. Leetcode_easy_653. Two Sum IV - Input is a BST; 完
- [LeetCode&Python] Problem 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 BST s ...
- 【leetcode】653. Two Sum IV - Input is a BST
Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...
- 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 BST s ...
随机推荐
- Ubuntu16.04 Liunx下同时安装Anaconda2与Anaconda3
先根据Ubuntu预装的python2.7来安装Anaconda2,然后将Anaconda3作为其环境安装在envs文件夹下. 重要提示:有一些软件需要py2.7的环境,比如XX-Net, 最好是先安 ...
- windows生成库文件
库文件的生成,包括静态库lib与动态库dll,需要改变编译输出的生成命令,可以一开始生成对应的库工程(或者在工程属性->常规->配置类型更改). 附基本对应命令: gcc –c -L .o ...
- vue源码分析之目录架构(一)
compiler compiler 目录包含 Vue.js 所有编译相关的代码.它包括把模板解析成 ast 语法树,ast 语法树优化,代码生成等功能 core core 目录包含了 Vue.js 的 ...
- jenkins添加类ubuntu/centos节点报错
前言:在jenkins添加ubuntu节点,发现启动代理报错 以下是报错: [SSH] Checking java version of /usr/java/latest/bin/java Could ...
- Notes for "Python in a Nutshell"
Introduction to Python Wrap C/C++ libraries into Python via Cython and CFFI. Python implementations ...
- UIWebView的常用方法
//webview导航栏类型enum UIWebViewNavigationType : Int { case LinkClicked case FormSubmitted case BackForw ...
- Consideration about improving mathematics study
In this article, I’ll present my ideas about how to improve mathematics study, which are the forewor ...
- 【Android】webview javascript 注入方法
Android中向webview注入js代码可以通过webview.loadUrl("javascript:xxx")来实现,然后就会执行javascript后面的代码. 但是当需 ...
- C++运算符重载——类型转换
类型转换函数能够实现把一个类 类型 转换成 基本数据类型(int.float.double.char等) 或者 另一个类 类型. 其定义形式如下,注意不能有返回值,不能有参数,只能返回要转换的数据类型 ...
- day13.装饰器进阶,迭代器
1.from functools import wraps 这个函数可以保留原来函数的属性 # from functools import wraps def car_time(fun): # @wr ...