楼主: lvvvvv
跳转到指定楼层
上一主题 下一主题
收起左侧

FB 8/19 面经

🔗
muybienw 2016-8-20 11:27:01 | 只看该作者
全局:
tree的recursion换成iteration处理,一般用stack都能解决吧(相当于手动用stack模拟recursion)。感觉这题可以是一个样的做法,换成post order访问,这样处理每个node的时候,左右孩子的信息都有了,而且最后一个处理的node一定是tree root。
回复

使用道具 举报

🔗
zzh730 2016-8-20 12:20:08 | 只看该作者
全局:
最近这个题很高频啊,其实dfs遍历,返回的时候返回lca和depth,每个node如果有大于一个子节点的depth相同就返回这个node,如果有一个子节点depth更深就返回个子节点lca,这个o(n)就可以了
回复

使用道具 举报

🔗
chen6145 2016-8-21 01:19:17 | 只看该作者
全局:
楼主有空分享分享design题呀
回复

使用道具 举报

🔗
Badger96 2016-8-21 04:06:56 | 只看该作者
全局:
这题好高频,前两天才看到有人面一样的
回复

使用道具 举报

🔗
Lilian1109 2016-8-23 03:16:05 | 只看该作者
全局:
zzh730 发表于 2016-8-20 12:20
最近这个题很高频啊,其实dfs遍历,返回的时候返回lca和depth,每个node如果有大于一个子节点的depth相同就 ...

这个貌似get到了, 贴一个我的代码, 是这么个意思吗?
  1. TreeNode* dfs(TreeNode * root, int & depth){
  2.     if(!root){
  3.         depth = 0;
  4.         return NULL;
  5.     }
  6.    
  7.     int ldepth, rdepth;
  8.     TreeNode * left = dfs(root->left, ldepth);
  9.     TreeNode * right = dfs(root->right, rdepth);
  10.     depth = 1 + max(ldepth, rdepth);
  11.    
  12.     if(ldepth == rdepth) return root;
  13.     else if(ldepth < rdepth) return right;
  14.     else return right;
  15. }

  16. int commonAncestor(TreeNode * root){
  17.     int depth;
  18.     TreeNode* LCA = dfs(root, depth);
  19.     cout << "Tree depth:" << depth << endl;
  20.     return LCA->val;
  21. }
复制代码
回复

使用道具 举报

🔗
Lilian1109 2016-8-23 03:20:34 | 只看该作者
全局:
一个很naive的想法, 可不可以BFS找到deepest nodes, 同时把每一个node记录其parent。
每个 node向上返回一步, 把它们的parent用一个set来记录, 迭代, 当set的 size为1的时候就是他们的lowest common ancestor。
回复

使用道具 举报

🔗
zzh730 2016-8-23 06:19:54 | 只看该作者
全局:
Lilian1109 发表于 2016-8-23 03:16
这个貌似get到了, 贴一个我的代码, 是这么个意思吗?

是的,题里是多叉树,稍稍改下就好了
回复

使用道具 举报

🔗
lovelysier613 2016-8-24 08:25:01 | 只看该作者
全局:
Lilian1109 发表于 2016-8-23 03:16
这个貌似get到了, 贴一个我的代码, 是这么个意思吗?

这个代码写的很好!
如果不让用recursion,可以用一个stack来做post order travesal,顺便把左右子树的高度算出来比较,keep track这个左右子树一样高度最大值的父节点,即为所求。
回复

使用道具 举报

🔗
 楼主| lvvvvv 2016-8-31 01:04:58 | 只看该作者
全局:
chen6145 发表于 2016-8-21 01:19
楼主有空分享分享design题呀

design  pocketmon ,  google search suggestion.
回复

使用道具 举报

🔗
zhuhai_ZFC 2016-8-31 02:05:43 | 只看该作者
全局:
感觉这个可以DFS解。返回值为当前子树上最深叶子节点的深度,和当前子树上的最深叶子节点的lca。
  1. private static class Solution {
  2.         private class ReturnVal {
  3.             public int depth;   //The depth of the deepest leaves on the current subtree
  4.             public TreeNode lca;//The lca of the deepest leaves on the current subtree

  5.             public ReturnVal(int d, TreeNode n) {
  6.                 depth = d;
  7.                 lca = n;
  8.             }
  9.         }

  10.         public TreeNode LowestCommonAncestorOfDeepestLeaves(TreeNode root) {
  11.             ReturnVal res = find(root, 0);
  12.             return res.lca;
  13.         }

  14.         private ReturnVal find(TreeNode root, int depth) {
  15.             if(root == null) {
  16.                 return new ReturnVal(-1, null);
  17.             } else {
  18.                 ReturnVal lRes = find(root.left, depth+1);
  19.                 ReturnVal rRes = find(root.right, depth+1);

  20.                 if(lRes.depth == rRes.depth) {
  21.                     return new ReturnVal(lRes.depth==-1?depth:lRes.depth, root);
  22.                 } else {
  23.                     return new ReturnVal(Math.max(rRes.depth, lRes.depth), rRes.depth>lRes.depth?rRes.lca:lRes.lca);
  24.                 }
  25.             }
  26.         }
  27.     }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表