由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - a question regarding finding all paths with a common sum
相关主题
请教CareerCup中的ROBOT MATRIX PATH那道题Find number of subtrees with the same value
问道amazon面试题问道150上的题:sum of path in binary tree
careercup 150 4.1 balanced tree 有错?matrix question
问CareerCup(第四版)一题的高效做法,谢谢!借人气问一道Samsung的题
cc150上面binary tree找所有sum==target的path,不一定从root出发Find largest common subtree
问两道facebook面试题请教一个面试问题,careercup上的
Careercup 4.9解释一下?rebuild a tree from inorder and level order
求教balanced binary tree的准确定义问个color tree的DP问题
相关话题的讨论汇总
话题: root话题: paths话题: sum话题: 路径话题: subtree
进入JobHunting版参与讨论
1 (共1页)
c**y
发帖数: 172
1
问题是找到一个binary tree中所有路径,对于每一条这样得路径,其成员节点的和等
于一个指定的值。附带的说明是符合条件的路径不是必须从root开始。下面是
CareerCup的问题说明和解答。
我的疑惑是这个solution给出的路径(们)确实可以不是从root开始,但是每一条路径
一定是沿着root到某一个叶子节点(即在较低level的端点一定是在一个以在较高level
端点为顶点的subtree里面),而不会出现路径的两端出现在两个不同的subtree中。
However,题的说明之中并没有指明路径的两端不可以出现在两个不同的subtree中。
如何修改下面的solution使得上面所说的路径可以被找到呢?Thanks
Problem:
You are given a binary tree in which each node contains a value. Design an
algorithm to print all paths which sum up to that value. Note that it can be
any path in the tree
I**********s
发帖数: 441
2
我一直觉得这不是subset sum问题吗? 是NPC的. 作为面试是不是难了点?
http://en.wikipedia.org/wiki/Subset_sum_problem
f*********5
发帖数: 576
3
void GetPath(node * root, vector&vec,int target)
{
if(!root) return;
if(vec.size()>0)
{ sum=0;
for(int i=vec.size()-1;i>=0;i--)
{ sum+=vec[i];
if(sum==target) { PRINT;break;}
}
}
vec.push_back(root->data);
if (root->left) GetPath(root->left,vec,target);
if (root->right) GetPath(root->right,vec,target);
return;
}

level
Design an

【在 c**y 的大作中提到】
: 问题是找到一个binary tree中所有路径,对于每一条这样得路径,其成员节点的和等
: 于一个指定的值。附带的说明是符合条件的路径不是必须从root开始。下面是
: CareerCup的问题说明和解答。
: 我的疑惑是这个solution给出的路径(们)确实可以不是从root开始,但是每一条路径
: 一定是沿着root到某一个叶子节点(即在较低level的端点一定是在一个以在较高level
: 端点为顶点的subtree里面),而不会出现路径的两端出现在两个不同的subtree中。
: However,题的说明之中并没有指明路径的两端不可以出现在两个不同的subtree中。
: 如何修改下面的solution使得上面所说的路径可以被找到呢?Thanks
: Problem:
: You are given a binary tree in which each node contains a value. Design an

M********5
发帖数: 715
4

呵呵,对头,这题用的就是recursion

【在 f*********5 的大作中提到】
: void GetPath(node * root, vector&vec,int target)
: {
: if(!root) return;
: if(vec.size()>0)
: { sum=0;
: for(int i=vec.size()-1;i>=0;i--)
: { sum+=vec[i];
: if(sum==target) { PRINT;break;}
: }
: }

c**y
发帖数: 172
5
Thanks for your solution. Yours is the same as the solution given in
CareerCup. So it shares the same problem as mentioned above, that is, it
only finds all paths such that one end must be in a subtree with the other
end as root. Can your solution find other paths in which two ends are cross
different subtrees? I will appreciate you very much if you could clarify an
answer for that case. Many thanks ahead.

【在 f*********5 的大作中提到】
: void GetPath(node * root, vector&vec,int target)
: {
: if(!root) return;
: if(vec.size()>0)
: { sum=0;
: for(int i=vec.size()-1;i>=0;i--)
: { sum+=vec[i];
: if(sum==target) { PRINT;break;}
: }
: }

1 (共1页)
进入JobHunting版参与讨论
相关主题
问个color tree的DP问题cc150上面binary tree找所有sum==target的path,不一定从root出发
A G interview question问两道facebook面试题
关于inordertraversal 的iterative wayCareercup 4.9解释一下?
问个google面试题求教balanced binary tree的准确定义
请教CareerCup中的ROBOT MATRIX PATH那道题Find number of subtrees with the same value
问道amazon面试题问道150上的题:sum of path in binary tree
careercup 150 4.1 balanced tree 有错?matrix question
问CareerCup(第四版)一题的高效做法,谢谢!借人气问一道Samsung的题
相关话题的讨论汇总
话题: root话题: paths话题: sum话题: 路径话题: subtree