int res = 0; publicintpathSum(int[] nums){ Node root = new Node(nums[0] % 10);
// 这个构建方法中,判断左右子树那块没太搞懂,直接抄了 for (int num: nums) { if (num == nums[0]) continue; int depth = num / 100, pos = num / 10 % 10, val = num % 10; pos--; Node cur = root; for (int d = depth - 2; d >= 0; --d) { if (pos < 1<<d) { if (cur.left == null) cur.left = new Node(val); cur = cur.left; } else { if (cur.right == null) cur.right = new Node(val); cur = cur.right; } pos %= 1<<d; } }
dfs(root, 0); return res; }
privatevoiddfs(TreeNode root, int currSum){ if (root == null) { return; }
currSum += root.val; if (root.left == null && root.right == null) { res += currSum; return; }
classSolution{ int ans = 0; Map<Integer, Integer> values; publicintpathSum(int[] nums){ values = new HashMap(); for (int num: nums) values.put(num / 10, num % 10);
dfs(nums[0] / 10, 0); return ans; }
publicvoiddfs(int node, int sum){ if (!values.containsKey(node)) return; sum += values.get(node);
int depth = node / 10, pos = node % 10; int left = (depth + 1) * 10 + 2 * pos - 1; int right = left + 1;
if (!values.containsKey(left) && !values.containsKey(right)) { ans += sum; } else { dfs(left, sum); dfs(right, sum); } } }