w*2
1 楼
从版上获益不少,昨天G的店面很不理想,也发下面经吧
阿三面试官到点了 还说要几分钟弄好麦克风。
问了一下bst的性质 就让我写个validate bst的 函数
我想这不是原题吗?就把之前写过的敲了一遍。
public boolean isValidBST(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(root==null) return true;
int[] last = new int[1];
last[0]=Integer.MIN_VALUE;
return isValidBST(root,last);
}
public boolean isValidBST(TreeNode root, int[] last){
if (root.left!=null){
if(root.val<=root.left.val || !isValidBST(root.left,last))
return false;
}
if (root.val<=last[0]) return false;
last[0]=root.val;
if (root.right!=null){
if(root.val>=root.right.val || !isValidBST(root.right,last))
return false;
}
return true;
}
然后他跟我说 有问题。(root.val<=root.left.val 其实可以不check 但写了也没错阿
)
举了个例子
8
/
4
\
10
说不行,..我说可以阿。
这样折腾了30分钟 之前他浪费了7-8分钟。
最后问我 thread process 的区别。
然后就结束了。
心情很沉重,朋友的推荐给力,但一电面就成这样了。。。
阿三面试官到点了 还说要几分钟弄好麦克风。
问了一下bst的性质 就让我写个validate bst的 函数
我想这不是原题吗?就把之前写过的敲了一遍。
public boolean isValidBST(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(root==null) return true;
int[] last = new int[1];
last[0]=Integer.MIN_VALUE;
return isValidBST(root,last);
}
public boolean isValidBST(TreeNode root, int[] last){
if (root.left!=null){
if(root.val<=root.left.val || !isValidBST(root.left,last))
return false;
}
if (root.val<=last[0]) return false;
last[0]=root.val;
if (root.right!=null){
if(root.val>=root.right.val || !isValidBST(root.right,last))
return false;
}
return true;
}
然后他跟我说 有问题。(root.val<=root.left.val 其实可以不check 但写了也没错阿
)
举了个例子
8
/
4
\
10
说不行,..我说可以阿。
这样折腾了30分钟 之前他浪费了7-8分钟。
最后问我 thread process 的区别。
然后就结束了。
心情很沉重,朋友的推荐给力,但一电面就成这样了。。。