目录结构
1.题目
给出一棵二叉树,其上每个结点的值都是 0 或 1 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。例如,如果路径为 0 -> 1 -> 1 -> 0 -> 1,那么它表示二进制数 01101,也就是 13 。
对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。
以 10^9 + 7 为模,返回这些数字之和。
示例:
输入:[1,0,1,0,1,0,1]
输出:22
解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
提示:
- 树中的结点数介于
1
和1000
之间。 - node.val 为
0
或1
。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sum-of-root-to-leaf-binary-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2.题解
递归。
public class Solution1022 {
@Test
public void test1022() {
TreeNode root = new TreeNode(1, new TreeNode(0, new TreeNode(0), new TreeNode(1)),
new TreeNode(1, new TreeNode(0), new TreeNode(1)));
System.out.println(sumRootToLeaf(root));
}
long totalSum = 0;
int mod = 1000000007;
public int sumRootToLeaf(TreeNode root) {
preOrder(root, 0);
return (int) (totalSum % mod);
}
public void preOrder(TreeNode root, int tmpSum) {
if (root != null) {
tmpSum = tmpSum * 2 + root.val;
if (root.left == null && root.right == null) {
totalSum += tmpSum;
} else {
preOrder(root.left, tmpSum);
preOrder(root.right, tmpSum);
}
}
}
}
- 时间复杂度:
- 空间复杂度: