12
返回列表 发新帖
楼主: lotustree86
跳转到指定楼层
上一主题 下一主题
收起左侧

FB 电面

🔗
tldxk 2016-3-12 10:26:18 | 只看该作者
全局:
楼主为什么要用count呢?还是说给定的参数就有count?求检查下我的代码:
  1. public TreeNode findSingleBST(Set<TreeNode> set) {
  2.     if (set == null) {
  3.         return null;
  4.     }

  5.     TreeNode root = null;
  6.     for (TreeNode node : new HashSet<>(set)) {
  7.         if (set.contains(node)) {
  8.             root = node;
  9.             // dfs
  10.             Stack<TreeNode> stack = new Stack<>();
  11.             stack.push(root);
  12.             while (!stack.isEmpty()) {
  13.                 TreeNode current = stack.pop();
  14.                 if (current != root) {
  15.                     set.remove(current);
  16.                 }
  17.                 if (current.left != null) {
  18.                     stack.push(current.left);
  19.                 }
  20.                 if (current.right != null) {
  21.                     stack.push(current.right);
  22.                 }
  23.             }
  24.         }
  25.     }

  26.     if (set.size() > 1) {
  27.         return null;
  28.     }
  29.     return root;
  30. }
复制代码
回复

使用道具 举报

🔗
sealove999 2016-4-18 10:22:44 | 只看该作者
全局:
lotustree86 发表于 2016-3-2 15:24
你这个方法不错,先找到根节点,再遍历
  1. public class Solution {
  2.   public TreeNode onetree(Set<TreeNode> set) {
  3.     Set<TreeNode> candidate = new HashSet<>(set);
  4.     for (TreeNode tn : set) {
  5.       if (tn.left != null) {
  6.         candidate.remove(tn.left);
  7.       }
  8.       if (tn.right != null) {
  9.         candidate.remove(tn.right);
  10.       }
  11.     }
  12.     for (TreeNode root : candidate) {
  13.       Set<TreeNode> nodes = new HashSet<>(set);
  14.       Deque<TreeNode> queue = new ArrayDeque<>();
  15.       queue.offer(root);
  16.       nodes.remove(root);
  17.       while (!queue.isEmpty()) {
  18.         TreeNode tn = queue.poll();
  19.         if (tn.left != null) {
  20.           queue.offer(tn.left);
  21.           nodes.remove(tn.left);
  22.         }
  23.         if (tn.right != null) {
  24.           queue.offer(tn.right);
  25.           nodes.remove(tn.right);
  26.         }
  27.       }
  28.       if (nodes.isEmpty())
  29.         return root;
  30.     }
  31.     return null;
  32.   }

  33.   public static void main(String[] args) {
  34.     Solution s = new Solution();
  35.     TreeNode tn1 = new TreeNode(1);
  36.     TreeNode tn2 = new TreeNode(2);
  37.     TreeNode tn3 = new TreeNode(3);
  38.     TreeNode tn4 = new TreeNode(4);
  39.     TreeNode tn5 = new TreeNode(5);
  40.     TreeNode tn6 = new TreeNode(6);
  41.     TreeNode tn7 = new TreeNode(7);
  42.     tn1.left = tn2;
  43.     tn1.right = tn3;
  44.     tn2.right = tn4;
  45.     tn5.left = tn6;
  46.     tn5.right = tn7;
  47.     Set<TreeNode> set1 = new HashSet<>();
  48.     set1.add(tn1);
  49.     set1.add(tn2);
  50.     set1.add(tn3);
  51.     set1.add(tn4);
  52.     Set<TreeNode> set2 = new HashSet<>();
  53.     set2.add(tn1);
  54.     set2.add(tn2);
  55.     set2.add(tn3);
  56.     set2.add(tn4);
  57.     set2.add(tn5);
  58.     set2.add(tn6);
  59.     set2.add(tn7);
  60.     System.out.println(s.onetree(set1).equals(tn1));
  61.     System.out.println(s.onetree(set2) == null);
  62.   }
  63. }
复制代码
回复

使用道具 举报

🔗
sjph 2016-7-31 17:44:02 | 只看该作者
全局:
tldxk 发表于 2016-3-12 10:26
楼主为什么要用count呢?还是说给定的参数就有count?求检查下我的代码:

请问楼上做法,这个没有判断是否为BST吧?包括大家的做法,都没看出在哪里判断是BST了。抱歉,求讲解!
回复

使用道具 举报

🔗
qesss 2016-9-29 01:54:51 | 只看该作者
全局:
第二题应该用拓扑排序的思想。
回复

使用道具 举报

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

本版积分规则

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