Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

二叉树中和为某一值的路径算法问题 #14

Closed
yuwengCipher opened this issue Dec 20, 2019 · 0 comments
Closed

二叉树中和为某一值的路径算法问题 #14

yuwengCipher opened this issue Dec 20, 2019 · 0 comments

Comments

@yuwengCipher
Copy link

function FindPathCore(node, expectNumber, stack, sum, result) {
stack.push(node.val);
sum += node.val;
if (!node.left && !node.right && sum === expectNumber) {
result.push(stack.slice(0));
}
if (node.left) {
FindPathCore(node.left, expectNumber, stack, sum, result);
}
if (node.right) {
FindPathCore(node.right, expectNumber, stack, sum, result);
}
stack.pop();
}

当stack[stack.length-1]存在并且没访问过时,stack.pop()会将右子树切断,造成结果项缺失。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant