74 字
1 分钟
哈夫曼树
2024-09-15
无标签
struct Node {
	w: int
	left: *Node
	right: *Node
	is_leaf() {
		return this.left === null && this.right === null 
	}
}
wpl(root: *Node, len: int = 0) -> int {
	if root === null {
		return 0
	} else if root.is_leaf() {
		return len * root.w
	} else {
		int ret = wpl(root.left, len + 1) + wpl(root.right, len + 1);
		root.w = root.left?.w ?? 0 + root.right?.w ?? 0
		return ret
	}
}