由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 再来题目
相关主题
Given a binary tree, find the maximum path sumCracking Coding Interview 4.8 求问
First Missing Positive on Leetcode二叉树如何判断一个节点是不是在另外两个节点的path上。。。。好像是个老题了。。求解?
Longest Consecutive Sequence 问题释疑leetcode 上 path sum 那道题 一问
Leet Code, three sum closestFB面经加求问
问道150上的题:sum of path in binary tree问个binary tree node path的概念问题
这个题做的对吗?Careercup 4.9解释一下?
cc150上面binary tree找所有sum==target的path,不一定从root出发大侠帮我看看这段程序
借人气问一道Samsung的题Binary Tree Maximum Path Sum
相关话题的讨论汇总
话题: sum话题: path话题: tree话题: array话题: valu
进入JobHunting版参与讨论
1 (共1页)
H*M
发帖数: 1268
1
Given a value and a binary search tree.
Print all the paths(if there exists more than one) which sum up to that valu
e. It can be any path in the tree. It doesn't have to be from the root.
我理解是这个path可以是其中任意一截,不用包括头尾
H*M
发帖数: 1268
2
感觉这题不太容易
就是一个unsorted array,求出所有consecutive sub array和为sum都不trivial

valu

【在 H*M 的大作中提到】
: Given a value and a binary search tree.
: Print all the paths(if there exists more than one) which sum up to that valu
: e. It can be any path in the tree. It doesn't have to be from the root.
: 我理解是这个path可以是其中任意一截,不用包括头尾

h***r
发帖数: 726
3
下面这个有线性解吧。
对原体来说,可能应该对长度循环: total time complexity O(n log^2 n). assume
tree
is balanced.
长度为1: 退化为search, trival, O(logn)
长度为2:
let's say the input is x, we use t.v, t.left.v t.right.v for the value at
node t, t.left, t.right
从根遍历树(using a queue),但是加上下面的一些speedup:
init: q.push_back(root), t = q.pop()
a = t.v + t.left.v
b = t.v + t.right.v
if (x < a) {
push_back(t.left);
} else if (x == a) {
output this path;
push_back(t.left.right);
} else if (x betwen a, b) {
pus

【在 H*M 的大作中提到】
: 感觉这题不太容易
: 就是一个unsorted array,求出所有consecutive sub array和为sum都不trivial
:
: valu

h***r
发帖数: 726
4
another simple O(n log^2 n) algorithm (assume tree is balanced)
for (each node) { // n
for each path from the node toward the root { // logn
output the path if the sum of the path == input value //
logn
}
}

tree

【在 h***r 的大作中提到】
: 下面这个有线性解吧。
: 对原体来说,可能应该对长度循环: total time complexity O(n log^2 n). assume
: tree
: is balanced.
: 长度为1: 退化为search, trival, O(logn)
: 长度为2:
: let's say the input is x, we use t.v, t.left.v t.right.v for the value at
: node t, t.left, t.right
: 从根遍历树(using a queue),但是加上下面的一些speedup:
: init: q.push_back(root), t = q.pop()

H*M
发帖数: 1268
5
are you kidding?

【在 h***r 的大作中提到】
: another simple O(n log^2 n) algorithm (assume tree is balanced)
: for (each node) { // n
: for each path from the node toward the root { // logn
: output the path if the sum of the path == input value //
: logn
: }
: }
:
: tree

S*********N
发帖数: 6151
6

valu
if it is sorted, then the given value will provide some boundary.

【在 H*M 的大作中提到】
: Given a value and a binary search tree.
: Print all the paths(if there exists more than one) which sum up to that valu
: e. It can be any path in the tree. It doesn't have to be from the root.
: 我理解是这个path可以是其中任意一截,不用包括头尾

b***e
发帖数: 1419
7
If all the numbers are non-negative, then it's trivial, just traverse
the list one with two pointers i and j, and bookkeeping the sum
between the ith element and the jth element. This takes O(n).

【在 H*M 的大作中提到】
: 感觉这题不太容易
: 就是一个unsorted array,求出所有consecutive sub array和为sum都不trivial
:
: valu

H*****L
发帖数: 5705
8
ft..这题没这么简单吧
这个方法不对,而且复杂度也不是O(n)

【在 b***e 的大作中提到】
: If all the numbers are non-negative, then it's trivial, just traverse
: the list one with two pointers i and j, and bookkeeping the sum
: between the ith element and the jth element. This takes O(n).

H*M
发帖数: 1268
9
If there can be negative numbers in the array,can we do this:
sum all elements to the left, s[i] = sum of a[j], j=0...i
O(n) here
then sort the sum_array s, for each element in s, s[i], binary_search the v
alue GIVEN_SUM + s[i] to see if that element is in the sum_array s or not. I
f yes, we find it.
O(nlgn)

【在 b***e 的大作中提到】
: If all the numbers are non-negative, then it's trivial, just traverse
: the list one with two pointers i and j, and bookkeeping the sum
: between the ith element and the jth element. This takes O(n).

b***e
发帖数: 1419
10
是.

the v
not. I

【在 H*M 的大作中提到】
: If there can be negative numbers in the array,can we do this:
: sum all elements to the left, s[i] = sum of a[j], j=0...i
: O(n) here
: then sort the sum_array s, for each element in s, s[i], binary_search the v
: alue GIVEN_SUM + s[i] to see if that element is in the sum_array s or not. I
: f yes, we find it.
: O(nlgn)

r**u
发帖数: 1567
11
这个path必须是一个非叶节点到叶子节点,还是任意俩个node of the tree都行?

valu

【在 H*M 的大作中提到】
: Given a value and a binary search tree.
: Print all the paths(if there exists more than one) which sum up to that valu
: e. It can be any path in the tree. It doesn't have to be from the root.
: 我理解是这个path可以是其中任意一截,不用包括头尾

p********7
发帖数: 549
12
what is a[i]? is it the in-order traverse array?

the v
not. I

【在 H*M 的大作中提到】
: If there can be negative numbers in the array,can we do this:
: sum all elements to the left, s[i] = sum of a[j], j=0...i
: O(n) here
: then sort the sum_array s, for each element in s, s[i], binary_search the v
: alue GIVEN_SUM + s[i] to see if that element is in the sum_array s or not. I
: f yes, we find it.
: O(nlgn)

s*******t
发帖数: 248
13
I don't think both of your idea will work.
But thanks for sharing your idea.

assume

【在 h***r 的大作中提到】
: 下面这个有线性解吧。
: 对原体来说,可能应该对长度循环: total time complexity O(n log^2 n). assume
: tree
: is balanced.
: 长度为1: 退化为search, trival, O(logn)
: 长度为2:
: let's say the input is x, we use t.v, t.left.v t.right.v for the value at
: node t, t.left, t.right
: 从根遍历树(using a queue),但是加上下面的一些speedup:
: init: q.push_back(root), t = q.pop()

1 (共1页)
进入JobHunting版参与讨论
相关主题
Binary Tree Maximum Path Sum问道150上的题:sum of path in binary tree
讨论一道LeetCode题:Binary Tree Maximum Path Sum这个题做的对吗?
Leetcode bst max path-----is this solution correct?cc150上面binary tree找所有sum==target的path,不一定从root出发
感觉leetcode的OJ有点太偏重DP了借人气问一道Samsung的题
Given a binary tree, find the maximum path sumCracking Coding Interview 4.8 求问
First Missing Positive on Leetcode二叉树如何判断一个节点是不是在另外两个节点的path上。。。。好像是个老题了。。求解?
Longest Consecutive Sequence 问题释疑leetcode 上 path sum 那道题 一问
Leet Code, three sum closestFB面经加求问
相关话题的讨论汇总
话题: sum话题: path话题: tree话题: array话题: valu