avatar
请推荐Boots产品# Fashion - 美丽时尚
p*a
1
private info是不能随便公布的
我同样对这样的话感到歉意
这个帖子 100包子
我求1000伪币 给10% 发包酬劳
有的pm我
avatar
h*3
2
以前面试中碰到过的。拿出来讨论,看看是否完整。
Given a binary tree, find the length of the longest path.
其实就是找树的直径:左子树高,右子树高,穿过树根的最长的path,这三个Value的最
大值
/* root of the tree, height*/
int dia(struct node *root,int* h){
if (root == null){
*h = 0;
return 0;
}

/* left tree height, right tree height*/
int lh = 0, rh = 0;
/* left subtree diameter, right subtree dia*/
int ld = 0, rd = 0;

ld = dia(root->left,&lh);
rd = dia(root->right,&rh);

*h = max(lh,rh) + 1;
return max(lh+rh+1,max(rd,ld))
}
time is O(n)
avatar
i*e
3
想买些带回国自己用,不知道这个牌子那些东西比较好?
avatar
E*r
4
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
l*8
5
我觉得没问题。
avatar
r*5
6
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
y*n
7
首先,我们需要明确所谓的path是否包含“逆行”的path。
如果“逆行”也算path的话,这段代码貌似有个bug。
如果输入是一个root,左右各一个叶节点,共三个节点。
这样的path长度是2,而这段代码应该会返回1。

【在 h******3 的大作中提到】
: 以前面试中碰到过的。拿出来讨论,看看是否完整。
: Given a binary tree, find the length of the longest path.
: 其实就是找树的直径:左子树高,右子树高,穿过树根的最长的path,这三个Value的最
: 大值
: /* root of the tree, height*/
: int dia(struct node *root,int* h){
: if (root == null){
: *h = 0;
: return 0;
: }

avatar
k*n
8
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
S*t
9
随便选个点,DFS到最远的叶子结点A,从这个叶子结点A再DFS到离该叶子结点最远的B
,AB就是最长路径。

【在 h******3 的大作中提到】
: 以前面试中碰到过的。拿出来讨论,看看是否完整。
: Given a binary tree, find the length of the longest path.
: 其实就是找树的直径:左子树高,右子树高,穿过树根的最长的path,这三个Value的最
: 大值
: /* root of the tree, height*/
: int dia(struct node *root,int* h){
: if (root == null){
: *h = 0;
: return 0;
: }

avatar
m*e
10
rererererere

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
y*n
11
不好意思,好像是我对length理解错了。 :)

【在 y****n 的大作中提到】
: 首先,我们需要明确所谓的path是否包含“逆行”的path。
: 如果“逆行”也算path的话,这段代码貌似有个bug。
: 如果输入是一个root,左右各一个叶节点,共三个节点。
: 这样的path长度是2,而这段代码应该会返回1。

avatar
f*0
12
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
h*3
13
三个节点 (root, left, right), length of longest path is 3. This code returns
3.

【在 y****n 的大作中提到】
: 首先,我们需要明确所谓的path是否包含“逆行”的path。
: 如果“逆行”也算path的话,这段代码貌似有个bug。
: 如果输入是一个root,左右各一个叶节点,共三个节点。
: 这样的path长度是2,而这段代码应该会返回1。

avatar
s*y
14
re
avatar
a*3
15
我对这个问题的理解有点不明白。首先,这个binary tree是不是一定要是一个rooted
tree,建立在rooted tree的基础之上谈的?也就是说,有一个vertex,也就是root,满足
id(v)=0. 如果这样的话,path就只能向着一个direction走,逆行是指什么呢?两个
vertices有两个双向的edge么?那样的话,不就create了一个cycle,不满足tree的定义
呀?tree是没有cycle的。
如果不是两个vertices之间有两个edge,而是一个的话,只能指向一个方向,怎么逆行
呢?逆行存在了以后,那谁又是root呢?
我觉得binary tree的最长path就是它的height吧?也就是从root到最远leaf的edge的
数?那这道题就是求binary tree height的问题了?

【在 y****n 的大作中提到】
: 首先,我们需要明确所谓的path是否包含“逆行”的path。
: 如果“逆行”也算path的话,这段代码貌似有个bug。
: 如果输入是一个root,左右各一个叶节点,共三个节点。
: 这样的path长度是2,而这段代码应该会返回1。

avatar
d*o
16
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
a*3
17
我怎么觉得不对呢,人家问的是path, 你这个得出的结果是walk.
我觉得最长path就是bt的height
int BinaryTree::height() {
return heightRec(root);
}
int BinaryTree::heightRec(Node *n) {
if (n==0) {
return -1;
} else {
int height_l = heightRec(n->left);
int height_r = heightRec(n->right);
int height_s = height_l;
if (height_r > height_l) {
height_s = height_r;
}
return 1+height_s;
}
}
BinaryTree::BinaryTree() {
root = 0;
}

B
★ 发自iPhone App: ChineseWeb 7.8

【在 S******t 的大作中提到】
: 随便选个点,DFS到最远的叶子结点A,从这个叶子结点A再DFS到离该叶子结点最远的B
: ,AB就是最长路径。

avatar
a*e
18
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
b*i
19
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
m*o
20
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
d*p
21
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
I*t
22
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
w*d
23
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
w*i
24
avatar
p*6
25
re baozi
avatar
s*r
26
re
avatar
p*n
27
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
t*t
28
re
avatar
s*m
29
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
g*9
30
re
avatar
c*n
31
re,pai
avatar
b*g
32
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*t
33
re
avatar
y*0
34
avatar
O*e
35
re
avatar
d*r
36
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
m*s
37
RE
avatar
T*u
38
re
avatar
c*0
39
re
avatar
ee
40
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
w*0
41
re
avatar
d*1
42
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*e
43

re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
R*V
44
RE
avatar
z*n
45
re
avatar
b*n
46
RE
avatar
S*l
47
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
t*8
48
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
i*o
49
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
R*8
50
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
b*n
51
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
d*l
52
re
avatar
b*e
53
re
avatar
b*r
54
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
x*s
55
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*g
56
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
h*d
57
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
a*d
58
re
avatar
N*7
59
re
avatar
m*o
60
re
avatar
M*S
61
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*r
62
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
f*e
63
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
g*a
64
re
avatar
c*a
65
re
avatar
s*y
66
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
z*i
67
re
avatar
d*g
68
re
avatar
w*s
69
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*r
70
re
avatar
a*y
71
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
b*r
72
re
avatar
m*d
73
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
a*n
74
re
avatar
h*5
75
re
avatar
d*n
76
re baozi
avatar
s*e
77
re
avatar
l*8
78
re
avatar
k*8
79
re
avatar
d*r
80
re
avatar
A*n
81
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
h*m
82
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
C*s
83
re

private info是不能随便公布的
我同样对这样的话感到歉意
这个帖子 100包子
我求1000伪币 给10% 发包酬劳
有的pm我

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
S*T
84
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
o*o
85
re
avatar
G*s
86
re
baozi

【在 E*******r 的大作中提到】
: re
avatar
a*i
87
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
Y*o
88
re
avatar
s*n
89
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
p*u
90
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
hs
91
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
H*C
92
re
avatar
d*0
93
re
avatar
c*t
94
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
D*s
95
re
avatar
w*r
96
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
l*s
97
re
avatar
W*t
98
re
avatar
l*8
99
re
avatar
P*r
100
re
avatar
s*y
101
日,早叫你发包子了.

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*a
102
Re
avatar
m*l
103
re 包子
avatar
a*t
104
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
t*f
105
re
avatar
a*l
106
re
avatar
z*0
107
re
avatar
b*o
108
re
avatar
L*9
109
100

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
a*e
110
re
avatar
s*x
111
re
avatar
b*t
112
rere
avatar
e*o
113
Re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
s*r
114
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
l*l
115
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
L*1
116
re
avatar
p*r
117
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
J*i
118
re

private info是不能随便公布的
我同样对这样的话感到歉意
这个帖子 100包子
我求1000伪币 给10% 发包酬劳
有的pm我

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
i*8
119
re
avatar
f*r
120
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*t
121
re
avatar
n*5
122
re
avatar
s*u
123
re
avatar
m*a
124
RE
avatar
e*i
125
re
avatar
r*g
126
re
avatar
t*r
127
re
avatar
h*a
128
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
s*8
129
re
avatar
s*0
130
re
avatar
u*k
131
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
L*i
132
avatar
s*t
133
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
z*q
134
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
i*t
135
pai baozi
avatar
z*e
136
100个早过了,昨天睡得太早

【在 i******t 的大作中提到】
: pai baozi
avatar
g*u
137
RE
avatar
t*y
138
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
c*l
139
re
avatar
P*r
140
re
avatar
z*0
141
re
avatar
l*n
142
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
s*j
143
re
avatar
a*s
144
re

private info是不能随便公布的
我同样对这样的话感到歉意
这个帖子 100包子
我求1000伪币 给10% 发包酬劳
有的pm我

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
j*1
145
re
avatar
t*1
146
re
avatar
l*i
147
rere
avatar
j*u
148
re
avatar
y*0
149
re
avatar
g*8
150


【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
w*a
151
re
avatar
y*8
152
re
avatar
B*N
153
re
avatar
m*o
154
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

avatar
m*a
155
re
avatar
w*t
156
re
avatar
n*n
157
re

【在 p***a 的大作中提到】
: private info是不能随便公布的
: 我同样对这样的话感到歉意
: 这个帖子 100包子
: 我求1000伪币 给10% 发包酬劳
: 有的pm我

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