二叉树最长路径 java,如何列出二叉树中最长的路径?

这里我们试图列出二叉树中最长的路径。例如

list_longest_path(None)

[]

list_longest_path(BinaryTree(5))

[5]

b1 = BinaryTree(7)

b2 = BinaryTree(3, BinaryTree(2), None)

b3 = BinaryTree(5, b2, b1)

list_longest_path(b3)

[5, 3, 2]

我的代码在底部。显然,代码返回树中的每个节点。在这里,我在如何同时使用max()时生成所有列表有困难?在class BinaryTree:

"""

A Binary Tree, i.e. arity 2.

=== Attributes ===

@param object data: data for this binary tree node

@param BinaryTree|None left: left child of this binary tree node

@param BinaryTree|None right: right child of this binary tree node

"""

def __init__(self, data, left=None, right=None):

"""

Create BinaryTree self with data and children left and right.

@param BinaryTree self: this binary tree

@param object data: data of this node

@param BinaryTree|None left: left child

@param BinaryTree|None right: right child

@rtype: None

"""

self.data, self.left, self.right = data, left, right

def __eq__(self, other):

"""

Return whether BinaryTree self is equivalent to other.

@param BinaryTree self: this binary tree

@param Any other: object to check equivalence to self

@rtype: bool

>>> BinaryTree(7).__eq__("seven")

False

>>> b1 = BinaryTree(7, BinaryTree(5))

>>> b1.__eq__(BinaryTree(7, BinaryTree(5), None))

True

"""

return (type(self) == type(other) and

self.data == other.data and

(self.left, self.right) == (other.left, other.right))

def __repr__(self):

"""

Represent BinaryTree (self) as a string that can be evaluated to

produce an equivalent BinaryTree.

@param BinaryTree self: this binary tree

@rtype: str

>>> BinaryTree(1, BinaryTree(2), BinaryTree(3))

BinaryTree(1, BinaryTree(2, None, None), BinaryTree(3, None, None))

"""

return "BinaryTree({}, {}, {})".format(repr(self.data),

repr(self.left),

repr(self.right))

def __str__(self, indent=""):

"""

Return a user-friendly string representing BinaryTree (self)

inorder. Indent by indent.

>>> b = BinaryTree(1, BinaryTree(2, BinaryTree(3)), BinaryTree(4))

>>> print(b)

4

1

2

3

"""

right_tree = (self.right.__str__(

indent + " ") if self.right else "")

left_tree = self.left.__str__(indent + " ") if self.left else ""

return (right_tree + "{}{}\n".format(indent, str(self.data)) +

left_tree)

def __contains__(self, value):

"""

Return whether tree rooted at node contains value.

@param BinaryTree self: binary tree to search for value

@param object value: value to search for

@rtype: bool

>>> BinaryTree(5, BinaryTree(7), BinaryTree(9)).__contains__(7)

True

"""

return (self.data == value or

(self.left and value in self.left) or

(self.right and value in self.right))

def list_longest_path(node):

"""

List the data in a longest path of node.

@param BinaryTree|None node: tree to list longest path of

@rtype: list[object]

>>> list_longest_path(None)

[]

>>> list_longest_path(BinaryTree(5))

[5]

>>> b1 = BinaryTree(7)

>>> b2 = BinaryTree(3, BinaryTree(2), None)

>>> b3 = BinaryTree(5, b2, b1)

>>> list_longest_path(b3)

[5, 3, 2]

"""

if node is None:

return []

elif not node.left and not node.right:

return [node]

else:

return [node]+list_longest_path(node.left)+list_longest_path(node.right)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值