【在 L********n 的大作中提到】 : 是准备不充分。因为突然的原因必须马上找工作,工作中做算法很少 : google电面被受打击... : Anyway, now I have figured it out
T*m
17 楼
泡茶的是专门的菊花吧?如杭白菊和野菊花。 你的花是自己种的,还是今年才买的?很好看。
【在 v******y 的大作中提到】 : 菊花可以泡茶啊?
z*n
18 楼
原来土共在搞藏独?怪不得需要进藏许可证呢。
【在 a****b 的大作中提到】 : 反共反到搞藏独么,joke版也是有自己的底线的
f*4
19 楼
找到工作后的第一件事情就是为找下一份工作做准备~
【在 L********n 的大作中提到】 : 是准备不充分。因为突然的原因必须马上找工作,工作中做算法很少 : google电面被受打击... : Anyway, now I have figured it out
L*a
20 楼
好看。怎么才开啊,我家的都谢了。
p*e
21 楼
期待赵云好诗
B*8
22 楼
How to remember path in BFT? How about using DFT (C#)? List MaxPath(Node r) { if (r==null) return new List(); List L = MaxPath(r.Left), R=MaxPath(r.Right); if (L.length > R.length) { L.Add(r); return L; } else { R.Add(r); return R; } }
depends on how longest path is defined. assume that he asked a path in undirected graph. the best solution for binary three is always recusrive solution since it is elegant and simple for the interview purpose. Hight (i) = 1 + max(Height(i.leftNode), Height(i.rightNode)) Height (null node) =0; LongthestPath(i) = 1+ Height(i.left) + Height(i.right).
【在 L********n 的大作中提到】 : 是准备不充分。因为突然的原因必须马上找工作,工作中做算法很少 : google电面被受打击... : Anyway, now I have figured it out
struct Node { Node* left; Node* right; }; int longest_path = 0; int GetTreeHeight(Node* root) { if (root == NULL) return 0; int left_height = GetTreeHeight(root->left); int right_height = GetTreeHeight(root->right); longest_path = max(longest_path, left_height + right_height);
return max(left_height, right_height) + 1; }
i*e
34 楼
Please note that it's not enough to return only the maximum depth. The question clearly specifies to return the longest path. Using DFS is the correct answer. BFS can work too, but requires either: 1) Parent pointer in each node to get the longest path from leaf up to the root. 2) An extra data structure to record the path from root to leaf, which is also pushed into the queue. DFS has a simple solution: void getLongestPathDFS(Node *root, vector &path, vector &longestPath) { if (!root) return; path.push_back(root); if (path.size() > longestPath.size()) longestPath = path; getLongestPathDFS(root->left, path, longestPath); getLongestPathDFS(root->right, path, longestPath); path.pop_back(); } vector getLongestPathDFS(Node *root) { vector longestPath; getLongestPathDFS(root, vector(), longestPath); return longestPath; } PS: It's not necessary to return a vector of nodes as the longest path. At each node, there's only two possible ways, left or right. Therefore, representing left as 0 and right as 1, you can use a bitmap data structure to record the maximum path from the root, which is more efficient in terms of speed and space. 一些常见面试题的答案与总结 - http://www.ihas1337code.com
【在 i**********e 的大作中提到】 : Please note that it's not enough to return only the maximum depth. The : question clearly specifies to return the longest path. : Using DFS is the correct answer. BFS can work too, but requires either: : 1) Parent pointer in each node to get the longest path from leaf up to the : root. : 2) An extra data structure to record the path from root to leaf, which is : also pushed into the queue. : DFS has a simple solution: : void getLongestPathDFS(Node *root, vector &path, : vector &longestPath) {