Redian新闻
>
<deal> Up to 75% off Sephora sale: select items
avatar
s*0
2
Sephora still takes up to 75% off select cosmetics, bath products, and
beauty accessories in its sale section. Shipping adds $5.95, or spend $50 or
more to qualify for free shipping. Of note, you'll have a choice of three
beauty samples for free at checkout. Some best bets:
•Dylan's Candy Lip Salve Stackable Set for $7.50
•Sephora Collection Retractable Foundation Brush #56 for $10
•Urban Decay Eyeshadow Duo (pictured) for $19
•Tarte Lights, Camera, Lashes! Mascara and Rejuvelash Lash Exhilirator
Duo for $19
•Too Faced Tanning Bed in a Tube and Snow Bunny Lip Bronzer for $21.50
link:
http://www.sephora.com/
对这些select items不是很了解,jms自己发掘吧!
avatar
C*5
3
Help :)
Is it too easy?
avatar
m*g
4
是不是和leetcode的tree deserialization 一样, 不过return判断条件是node level
not equal to current level. Current level 是recursion function 的输入参数
avatar
y*x
5
似乎可以用stack?
如果当前读到的level等于前一个节点的level+1,则当前节点是前一个节点的左节点。
否则,从栈中重新获取前一个节点,直到满足判断条件,则当前节点是前一个节点的右
节点。最后把当前节点压栈。循环直到栈为空,构建完成。
主要流程的伪代码大致如下(没考虑null等细节)
stack.push(root);
while(!stack.isEmpty()){
read value and level from input.
TreeNode cur = new TreeNode(value,level);
if(cur.level=pre.level+1){
pre.left=cur;
}else{
while(cur.level!=pre.level+1){
pre = stack.pop();
}
pre.right = cur;
}
pre = cur;
stack.push(cur);
}
avatar
f*x
6

能再稍微解释下题目么?没看懂题

【在 C**5 的大作中提到】
: Help :)
: Is it too easy?

avatar
A*o
7
贴个递归的完整代码,抛砖引玉了
#include
#include
#include
#include
using namespace std;
// Interview question: Rebuild a tree with DFS output with level
// A, 0, B, 1, C, 2, D, 2
struct TreeNode {
string id;
TreeNode(string a) {
id = a;
}
vector children;
};
void rebuildImpl(istringstream & iss, TreeNode *&father, string id, int
level) {
TreeNode *p = new TreeNode(id);
if (!father) {
father = p;
}
else {
father->children.push_back(p);
}
string nextId;
int nextLevel;
if (iss >> nextId >> nextLevel) {
if (nextLevel == level) {
rebuildImpl(iss, father, nextId, nextLevel);
}
else { // l == level+1
rebuildImpl(iss, p, nextId, nextLevel);
}
}
}
TreeNode* rebuild(const string &seq) {
istringstream iss(seq);
string id;
int level;
TreeNode *root = NULL;
if (iss >> id >> level) {
rebuildImpl(iss, root, id, level);
}
return root;
}
void dfs(TreeNode *node, int level) {
if (!node) {
return;
}
cout << node->id << " " << level << " ";
for (int i = 0; i < node->children.size(); i++) {
dfs(node->children[i], level+1);
}
}
int main() {
string trail = "A 0 B 1 C 2 D 2";
TreeNode *root = rebuild(trail);
dfs(root, 0); // should print same trail
cout << endl;
return 0;
}
avatar
h*t
8
这个输入怎么能判断B是A的左孩子还是右孩子?
还是说题目的输入本就允许有些情况下 rebuild的tree 不唯一?

【在 C**5 的大作中提到】
: A, 0, B, 1, C, 2, D, 2
avatar
p*2
9

没说是binary tree

【在 h****t 的大作中提到】
: 这个输入怎么能判断B是A的左孩子还是右孩子?
: 还是说题目的输入本就允许有些情况下 rebuild的tree 不唯一?

avatar
p*2
10
rebuild = (arr)->
stack = []
while arr.length > 0
[ch, lv] = arr
if lv is stack.length
node = {val: ch, children:[]}
stack[-1..][0]?.children.push(node)
stack.push(node)
arr = arr[2..]
else
stack.pop()
stack[0]
avatar
l*n
11
可以用hashmap,(K, V)等于(lvl, val),每次同一层有了新节点就更新hashmap中对应
lvl的元素。下一层减一就能找到他的parent node。

【在 C**5 的大作中提到】
: A, 0, B, 1, C, 2, D, 2
avatar
q*m
12
没有考虑 nextLevel < level的情况

【在 A*****o 的大作中提到】
: 贴个递归的完整代码,抛砖引玉了
: #include
: #include
: #include
: #include
: using namespace std;
: // Interview question: Rebuild a tree with DFS output with level
: // A, 0, B, 1, C, 2, D, 2
: struct TreeNode {
: string id;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。