查找BST中所有键位于给定范围内的子树(Find all subtrees in a BST whose keys lie in a given range)
在最近的一次采访中,我给出了这个问题:给定一个BST,其节点包含一个Integer值,找到所有节点落在整数X(min)和Y(max)之间的子树,其中X <Y。 这些子树不能相互重叠。
我已经解决了这个问题的变化,例如 - 打印一个BST的键落在一个给定的范围内。 但无法弄清楚这一点,因为它涉及到查找主图/树的所有连接的子图,以满足非常特定的约束条件。 任何指针/帮助/伪代码表示赞赏。
添加注释 -
- 该问题将节点的数据结构定义为具有左指针,右指针和整数值。 没有办法标记节点。
- 被问到用Java解决这个问题。
- 当我说子树/子图时,我的意思是连接的一组节点,而不是不相交节点的列表。 对困惑感到抱歉。
I was given this question during a recent interview: Given a BST whose nodes contains an Integer as value, find all subtrees whose nodes fall between integers X (min) and Y (max), where X<Y. These subtrees cannot overlap each other.
I have solved variations of this problem, example - print keys of a BST that fall in a given range. But couldn't figure this one out, since it involves finding all connected sub-graphs of the main graph/tree that satisfy very specific constraints. Any pointers/help/pseudo code is appreciated.
Added notes -
- The problem defined the datastructure of node as having a left pointer, right pointer, and a integer value. There was no way to mark a node.
- Was asked to solve this in Java.
- When i said subtree/subgraph, i meant a connected set of nodes, not a list of disjoint nodes. sorry for the confusion.
原文:https://stackoverflow.com/questions/31096354
最满意答案
如果你想知道在这两个字典中有多少值匹配,你应该说:)
也许这样的事情:
shared_items = set(x.items()) & set(y.items()) print len(shared_items)
If you want to know how many values match in both the dictionaries, you should have said that :)
Maybe something like this:
shared_items = {k: x[k] for k in x if k in y and x[k] == y[k]} print len(shared_items)
相关问答
更多-
通过使用python比较来自两个字典的值来创建字典(Create a dictionary by comparing values from two dictionaries using python)[2023-10-09]
你可以字典理解: {k1:datetime.now() - v1 if (k1 not in dict2 or v1 > dict2[k1]) else 'Old record' for k1,v1 in dict1.iteritems()} 它说“如果k1 不在 dict2 , 或者它存在并且dict1[k1] > dict2[k1]则执行datetime.now() - dict1[k1] 。否则,”旧记录“。” You could dictionar ... -
您可以轻松计算集合,从而从密钥创建集合并将其用于交集: keys_a = set(dict_a.keys()) keys_b = set(dict_b.keys()) intersection = keys_a & keys_b # '&' operator is used for set intersection You can easily calculate the intersection of sets, so create sets from the keys and use them for ...
-
如果你想知道在这两个字典中有多少值匹配,你应该说:) 也许这样的事情: shared_items = set(x.items()) & set(y.items()) print len(shared_items) If you want to know how many values match in both the dictionaries, you should have said that :) Maybe something like this: shared_items = {k: x[k] ...
-
比较字典(Python)(Comparing Dictionaries (Python))[2021-11-27]
使用all和collections.Counter这是一个高性能替代计数dict : > from collections import Counter > string_1 = 'hello' > string_2 = 'ello' > c1, c2 = map(Counter, (string_1, string_2)) # c1, c2 = Counter(string_1), Counter(string_2) > all(c1[c] >= c2[c] for c in c2) True 您 ... -
from collections import Counter c1 = Counter({'item1': 4, 'item2':2, 'item3': 5, 'item4': 3}) c2 = Counter({'item1': 6, 'item2':2, 'item3': 1, 'item5': 9}) c3 = {} for c in (c1, c2): for k,v in c.iteritems(): c3.setdefault(k, []).append(v) c3现 ...
-
如果我说得对 你可以使用list comprehension 码: lst=[{"a":2,"b":3,"c":4},{"b":4}] [a for a in lst[0] if a in lst[1]] ['b'] 这样做与list comprehension 码: lst=[{"a":2,"b":3,"c":4},{"b":4}] for a in lst[0]: if a in lst[1]]: print a 输出: b 操作: 当你循环遍历字典时,你循环遍历字典的键, ...
-
如何迭代两个字典并将它们保存在python的列表中(How to iterate over two dictionaries and save them in a list in python)[2023-05-31]
您可以分三步形成结果。 首先创建一个列表,其中包含通过创建键的交集而存在于两个字典上的键。 然后添加只存在于第一个密钥的密钥,最后添加仅在第二个字典中的密钥。 d = {'d': 2, 'c': 6, 'a': 1, 'b': 3} d2 = {'d': 9, 'b': 5, 'a': 2, 'k': 10} result = [[k, d[k], d2[k]] for k in d.keys() & d2.keys()] result.extend([k, d[k], 0] for k in d if ... -
这与单行词典理解一样简单。 >>> {k : {d1[k] : d2[k]} for k in d1.keys() & d2.keys()} { "key_2": { "BR": " signinfication of BR" }, "key_1": { "AR": "signinfication of AR" }, "key_3": { "CR": " signinfication of CR" } } ...
-
def checkList(lst): for item in lst: if "A" not in item.keys(): print item checkList(listOne) checkList(listTwo) Ok, i figured it out how to solve it (in a very, very ugly way): listOne = [{'A':'a','B':'b','C':'c'},{'A':'aa','B':'b ...
-
你可以压缩这些词组: for k, k2 in zip(price,inventory): print(price[k]*inventory[k2]) 即使您的代码工作,您将访问键而不是值,因此您需要使用上面的每个键访问dict值。 如果您使用的是python2,可以使用itertools.izip : from itertools import izip for k, k2 in izip(price,inventory): print(price[k],inventory[k2]) ...