- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图在二叉树中打印所有可能的路径。我能够打印所有根到叶的路径,但无法弄清楚如何添加叶到叶的路径。(我对根到叶使用预序遍历)。所以,基本上:
如果我的树是
6
/ \
4 0
/ \ \
1 3 1
如果要打印所有路径的代码:
6,4,1
6,4,3
6,0,1
1,4,6,0,1
3,4,6,0,1
1,4,3
4,6,0
4,6,0,1
etc.
任何人都可以帮我解决这个二叉树吗?非常感谢您的帮助,因为我是这个社会和 Python/Java 的新手。
谢谢
最佳答案
树的一个显着特性是对于节点 (x, y)
的每个组合,都存在从 x
到 y 的唯一非重复路径
。特别是,可以通过找到 x
和 y
的第一个共同祖先 z
并采用路径 x -> z + z -> y
.
因此找到所有路径的算法可能如下所示
for each pair of distinct nodes x, y in the tree:
find all common ancestors of x and y
let z be the lowest common acestor in the tree
let p be the path from x to z
append the path from z to y to p, excluding the duplicate z
print p
这是一种面向对象的方法,它实现了完成上述任务所需的方法。
class Tree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
self.parent = None
if left is not None:
left.parent = self
if right is not None:
right.parent = self
def __iter__(self):
"""Return a left-to-right iterator on the tree"""
if self.left:
yield from iter(self.left)
yield self
if self.right:
yield from iter(self.right)
def __repr__(self):
return str(self.value)
def get_ancestors(self):
"""Return a list of all ancestors including itself"""
ancestors = {self}
parent = self.parent
while parent:
ancestors.add(parent)
parent = parent.parent
return ancestors
def get_path_to_ancestor(self, ancestor):
"""
Return the path from self to ancestor as a list
output format: [self, ..., ancestor]
"""
path = []
parent = self
try:
while True:
path.append(parent)
if parent is ancestor:
break
else:
parent = parent.parent
except AttributeError:
return None
return path
def get_path_to(self, other):
"""
Return the path from self to other as a list
output format: [self, ..., first common acestor, ..., other]
"""
common_ancestors = self.get_ancestors() & other.get_ancestors()
first_common_ancestor = {
a for a in common_ancestors
if a.left not in common_ancestors and a.right not in common_ancestors
}.pop()
return self.get_path_to_ancestor(first_common_ancestor)\
+ list(reversed(other.get_path_to_ancestor(first_common_ancestor)))[1:]
以下是它如何应用于您作为示例提供的树。
tree = Tree(
6,
Tree(4,
Tree(1),
Tree(3)),
Tree(0,
Tree(1)))
nodes = list(tree)
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
print([t for t in nodes[i].get_path_to(nodes[j])])
这是打印的所有路径。
[1, 4]
[1, 4, 3]
[1, 4, 6]
[1, 4, 6, 0, 1]
[1, 4, 6, 0]
[4, 3]
[4, 6]
[4, 6, 0, 1]
[4, 6, 0]
[3, 4, 6]
[3, 4, 6, 0, 1]
[3, 4, 6, 0]
[6, 0, 1]
[6, 0]
[1, 0]
关于python - 打印二叉树中所有可能的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425656/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!