求二叉树的所有路径

二叉树的路径:从根结点到叶子结点
在这里插入图片描述
思路:初始化2个列表,一个存储到达本节点之前的节点,另一个存储所有的路径

# 构造一棵树
class Node(object):
    """
    节点
    """
    def __init__(self, left, right, data):
        self.data = data
        self.left = left
        self.right = right


class Tree(object):
    def add_root(self, node):
        self.root = node

    def add_left_tree(self, node, n_node):
        node.left = n_node

    def add_right_tree(self, node, n_node):
        node.right = n_node


tree = Tree()
node = Node(None, None, 15)
tree.add_root(node)

node_8_22 = Node(None, None, 22)
node_8_23 = Node(None, None, 23)
node_10_18 = Node(None, None, 18)

node_40_9 = Node(None, None, 9)
node_40_12 = Node(None, None, 12)

node_1 = Node(node_8_22, node_8_23, 8)
node_2 = Node(None, node_10_18, 10)
node_3 = Node(None, None, 20)

node_4 = Node(node_40_9, node_40_12, 40)
node_5 = Node(None, None, 6)


tree.add_left_tree(tree.root, node_3)
tree.add_left_tree(tree.root.left, node_1)
tree.add_right_tree(tree.root.left, node_2)

tree.add_right_tree(tree.root, node_5)
tree.add_right_tree(tree.root.right, node_4)


def get_all_path(node, before_nodes, paths):
    """获取路径“”“
    new_str = copy.deepcopy(before_nodes)
    if node.left is None and node.right is None:
        paths.append(before_nodes)
        return paths

    if node.left:
        before_nodes.append(node.left.data)
        get_all_path(node.left, before_nodes, paths)

    if node.right:
        new_str.append(node.right.data)
        get_all_path(node.right, new_str, paths)

    return paths


def binary_tree_path():
    before_nodes = []
    paths = []
    before_nodes.append(tree.root.data)
    get_all_path(tree.root, before_nodes, paths)
    return paths


if __name__ == "__main__":
    paths = binary_tree_path()
    print("paths: ", paths)

### 二叉树的带权路径长度(WPL)计算方法 二叉树的带权路径长度(Weighted Path Length, WPL)是指所有叶子节点的权值乘以其到根节点的路径长度之和。具体来说,每个叶子节点的贡献是其权值与其到根节点的路径长度的乘积,而路径长度是指从根节点到该叶子节点所经过的边的数量。 #### 递归方法计算 WPL 可以通过递归的方式来计算二叉树的 WPL。递归的基本思想是:如果当前节点是叶子节点,则直接返回该节点的权值乘以其深度;否则,递归计算左子树和右子树的 WPL,并将它们相加。以下是 C++ 和 C 语言的实现示例。 ##### C++ 实现 ```cpp class Solution { public: int WPL(TreeNode* root, int depth) { int lwpl = 0; int rwpl = 0; // 如果是叶子节点,计算当前叶子节点的 WPL if (root->left == NULL && root->right == NULL) { return root->val * depth; } // 递归计算左子树和右子树的 WPL if (root->left != NULL) { lwpl = WPL(root->left, depth + 1); } if (root->right != NULL) { rwpl = WPL(root->right, depth + 1); } return lwpl + rwpl; } int pathSum(TreeNode* root) { return WPL(root, 0); } }; ``` ##### C 实现 ```c int WPL(BiTree root) { return wpl_PreOrder(root, 0); } int wpl_PreOrder(BiTree root, int deep) { static int wpl = 0; if (root->lchild == NULL && root->rchild == NULL) { wpl += deep * root->weight; } if (root->lchild != NULL) { wpl_PreOrder(root->lchild, deep + 1); } if (root->rchild != NULL) { wpl_PreOrder(root->rchild, deep + 1); } return wpl; } ``` #### 非递归方法计算 WPL 除了递归方法,还可以使用非递归的方式(如深度优先搜索 DFS)来计算 WPL。这种方法通常使用栈来模拟递归过程,逐层遍历二叉树的节点,并记录每个节点的深度。 ### 哈夫曼树与 WPL 的关系 哈夫曼树是一种特殊的二叉树,它是由给定的一组带权叶子节点构造出的最优二叉树,具有最小的 WPL。哈夫曼树广泛应用于数据压缩领域,例如哈夫曼编码。[^3] ### 时间复杂度分析 递归或非递归方法的时间复杂度均为 $O(n)$,其中 $n$ 是二叉树中的节点总数。这是因为每个节点都会被访问一次,且每次访问的时间复杂度是常数级的。 ### 总结 二叉树的 WPL 是衡量二叉树结构优劣的一个重要指标,尤其在哈夫曼编码等应用场景中至关重要。通过递归或非递归的方法,可以高效地计算出二叉树的 WPL。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值