avatar
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 的区别。
然后就结束了。
心情很沉重,朋友的推荐给力,但一电面就成这样了。。。
avatar
w*e
2
如果只有一个root节点且值为MIN_VALUE岂不是输出false?

reused

【在 w**2 的大作中提到】
: 从版上获益不少,昨天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;

avatar
a*m
3
电面是怎么做代码题的 ?
avatar
J*3
4
google doc

【在 a***m 的大作中提到】
: 电面是怎么做代码题的 ?
avatar
w*2
5
你说的是对的。
我在面试中都跟此烙印说了 可以用node数组而不是int 数组,initial的时候放个
null 他说that is fine

【在 w*****e 的大作中提到】
: 如果只有一个root节点且值为MIN_VALUE岂不是输出false?
:
: reused

avatar
c*0
6
bool isBST(node* cur, int lowBound, int highBound){
if(cur){
if(cur->value > lowBound && cur->value < highBound)
return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
right, cur->value, highBound);
else
return false;
}
return true;
}
请指教。
avatar
q*8
7
这个写的有点乱。。。
avatar
w*2
8
回来之后 我朋友也给我写了这个解 用range去限定。
比我的简单 也clean
可能此烙印拿的也是这个解吧。但是说我的不对还是有点过了。
我当时自己练题,也没去找最好的或者最clean的解。

【在 c***0 的大作中提到】
: bool isBST(node* cur, int lowBound, int highBound){
: if(cur){
: if(cur->value > lowBound && cur->value < highBound)
: return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
: right, cur->value, highBound);
: else
: return false;
: }
: return true;
: }

avatar
k*4
9
这个也是你那本书上推荐的解法之一,但是总有个担心,万一BST中最小的元素等于
lowBound的初始值怎么办?
试着贴个刚在leetcode通过的代码
bool isBST(TreeNode*root, TreeNode*&pre)
{
if(root == NULL)
return true;
if(!isBST(root->left, pre) || (pre != NULL && pre->val >= root->val)
)
return false;
pre = root;
return isBST(root->right, pre);

}
bool isValidBST(TreeNode *root) {
TreeNode *pre = NULL;
return isBST(root,pre);
}

【在 c***0 的大作中提到】
: bool isBST(node* cur, int lowBound, int highBound){
: if(cur){
: if(cur->value > lowBound && cur->value < highBound)
: return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
: right, cur->value, highBound);
: else
: return false;
: }
: return true;
: }

avatar
D*d
10
一行代码搞掂
initial call: bool IsBST(root, INT_MIN - 1ll, INT_MAX + 1ll);
bool IsBST(Tree* root, long long MIN, long long MAX){
return root == 0? true: root->val < MAX && root->val > MIN && IsBST(root->
left, MIN, root->val) && IsBST(root->right, root->val, MAX);
}
avatar
w*2
11
nb 把限定range的逻辑简化成一句
我怕是能写出这个,烙印也可能说是错的。。

【在 D**********d 的大作中提到】
: 一行代码搞掂
: initial call: bool IsBST(root, INT_MIN - 1ll, INT_MAX + 1ll);
: bool IsBST(Tree* root, long long MIN, long long MAX){
: return root == 0? true: root->val < MAX && root->val > MIN && IsBST(root->
: left, MIN, root->val) && IsBST(root->right, root->val, MAX);
: }

avatar
s*w
12
梦鸟你真牛,学习了

【在 D**********d 的大作中提到】
: 一行代码搞掂
: initial call: bool IsBST(root, INT_MIN - 1ll, INT_MAX + 1ll);
: bool IsBST(Tree* root, long long MIN, long long MAX){
: return root == 0? true: root->val < MAX && root->val > MIN && IsBST(root->
: left, MIN, root->val) && IsBST(root->right, root->val, MAX);
: }

avatar
w*2
13
从版上获益不少,昨天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 的区别。
然后就结束了。
心情很沉重,朋友的推荐给力,但一电面就成这样了。。。
avatar
w*e
14
如果只有一个root节点且值为MIN_VALUE岂不是输出false?

reused

【在 w**2 的大作中提到】
: 从版上获益不少,昨天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;

avatar
a*m
15
电面是怎么做代码题的 ?
avatar
J*3
16
google doc

【在 a***m 的大作中提到】
: 电面是怎么做代码题的 ?
avatar
w*2
17
你说的是对的。
我在面试中都跟此烙印说了 可以用node数组而不是int 数组,initial的时候放个
null 他说that is fine

【在 w*****e 的大作中提到】
: 如果只有一个root节点且值为MIN_VALUE岂不是输出false?
:
: reused

avatar
c*0
18
bool isBST(node* cur, int lowBound, int highBound){
if(cur){
if(cur->value > lowBound && cur->value < highBound)
return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
right, cur->value, highBound);
else
return false;
}
return true;
}
请指教。
avatar
q*8
19
这个写的有点乱。。。
avatar
w*2
20
回来之后 我朋友也给我写了这个解 用range去限定。
比我的简单 也clean
可能此烙印拿的也是这个解吧。但是说我的不对还是有点过了。
我当时自己练题,也没去找最好的或者最clean的解。

【在 c***0 的大作中提到】
: bool isBST(node* cur, int lowBound, int highBound){
: if(cur){
: if(cur->value > lowBound && cur->value < highBound)
: return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
: right, cur->value, highBound);
: else
: return false;
: }
: return true;
: }

avatar
k*4
21
这个也是你那本书上推荐的解法之一,但是总有个担心,万一BST中最小的元素等于
lowBound的初始值怎么办?
试着贴个刚在leetcode通过的代码
bool isBST(TreeNode*root, TreeNode*&pre)
{
if(root == NULL)
return true;
if(!isBST(root->left, pre) || (pre != NULL && pre->val >= root->val)
)
return false;
pre = root;
return isBST(root->right, pre);

}
bool isValidBST(TreeNode *root) {
TreeNode *pre = NULL;
return isBST(root,pre);
}

【在 c***0 的大作中提到】
: bool isBST(node* cur, int lowBound, int highBound){
: if(cur){
: if(cur->value > lowBound && cur->value < highBound)
: return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
: right, cur->value, highBound);
: else
: return false;
: }
: return true;
: }

avatar
D*d
22
一行代码搞掂
initial call: bool IsBST(root, INT_MIN - 1ll, INT_MAX + 1ll);
bool IsBST(Tree* root, long long MIN, long long MAX){
return root == 0? true: root->val < MAX && root->val > MIN && IsBST(root->
left, MIN, root->val) && IsBST(root->right, root->val, MAX);
}
avatar
w*2
23
nb 把限定range的逻辑简化成一句
我怕是能写出这个,烙印也可能说是错的。。

【在 D**********d 的大作中提到】
: 一行代码搞掂
: initial call: bool IsBST(root, INT_MIN - 1ll, INT_MAX + 1ll);
: bool IsBST(Tree* root, long long MIN, long long MAX){
: return root == 0? true: root->val < MAX && root->val > MIN && IsBST(root->
: left, MIN, root->val) && IsBST(root->right, root->val, MAX);
: }

avatar
s*w
24
梦鸟你真牛,学习了

【在 D**********d 的大作中提到】
: 一行代码搞掂
: initial call: bool IsBST(root, INT_MIN - 1ll, INT_MAX + 1ll);
: bool IsBST(Tree* root, long long MIN, long long MAX){
: return root == 0? true: root->val < MAX && root->val > MIN && IsBST(root->
: left, MIN, root->val) && IsBST(root->right, root->val, MAX);
: }

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