Paths with Sum II
约 1 分钟· 本文阅读 次
本页目录
Paths with Sum II
You are given a binary tree in which each node contains an integer value (which might be positive or negative). Design an algorithm to count the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes)
Solution
暂时只想到暴力法
Code
// Brute Force
int countPath(TreeNode root, int sum){
if (root == null)
return 0;
int pathsRoot = countPathFromNode(root, sum, 0);
int pathsLeft = countPath(root.left, sum);
int pathsRight = countPath(root.right, sum);
return pathsRoot + pathsLeft + pathsRight;
}
int countPathFromNode(TreeNode node, int sum, int curSum){
if (node == null){
return 0;
}
curSum += node.data;
int totalPath = 0;
if (curSum == sum){
totalPath++;
}
totalPath += countPathFromNode(node.left, sum, curSum);
totalPath += countPathFromNode(node.right, sum, curSum);
return totalPath;
}相关文章
- All Path2016-01-14
- Balance Tree2016-01-14
- Binary Search Tree Iterator2016-01-14
- Binary Tree Depth2016-01-14
已恢复到上次阅读位置