C
C#15mo ago
morry329#

❔ How come shall the data type be int, not TreeNode?

https://leetcode.com/problems/validate-binary-search-tree/?envType=study-plan&id=level-1 It's about this question I have tried the below solution
public class Solution {
public bool IsValidBST(TreeNode root) {
List<TreeNode> l = new List<TreeNode>();
for(int i = 0; i < l.Count-1; i++){
if(l[i] >= l[i+1]){
return false;
}

}
return Helper(root,l);
}

public List<TreeNode> Helper(TreeNode root, List<TreeNode> l){
List<TreeNode> l = new List<TreeNode>();

if(root != null){

return l;
}

l.Add(Helper(root.left));
l.Add(root.val);
l.Add(Helper(root.right));
return l;


}
}
public class Solution {
public bool IsValidBST(TreeNode root) {
List<TreeNode> l = new List<TreeNode>();
for(int i = 0; i < l.Count-1; i++){
if(l[i] >= l[i+1]){
return false;
}

}
return Helper(root,l);
}

public List<TreeNode> Helper(TreeNode root, List<TreeNode> l){
List<TreeNode> l = new List<TreeNode>();

if(root != null){

return l;
}

l.Add(Helper(root.left));
l.Add(root.val);
l.Add(Helper(root.right));
return l;


}
}
` I know the logic is not right here, but some answers like this one set the data type of the List variable to int, not TreeNode` like I did. : https://leetcode.com/problems/validate-binary-search-tree/solutions/3327674/simple-c-code/?envType=study-plan&id=level-1&languageTags=csharp Could anyone kindly explain to me why it is int, not TreeNode?
LeetCode
Validate Binary Search Tree - LeetCode
Can you solve this real interview question? Validate Binary Search Tree - Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: * The left subtree of a node contains only nodes with keys less than the node's key. * The right subtree of a node contains only nodes with keys gre...
LeetCode
Simple C# code - Validate Binary Search Tree - LeetCode
View VikasSinghThakur07's solution of Validate Binary Search Tree on LeetCode, the world's largest programming community.
1 Reply
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.