I was struggling with this question for many times, and I have solved it every time, because I understand the concept. but I can never get to a good concise solution which is easy to follow.
In a coding interview I always choke when I see this question.
This is the best solution I was able to come up with.
Can the code be written even more simple and easier to follow?
using GraphsQuestions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TreeQuestions
{
/// <summary>
/// https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/625/
/// </summary>
/// check the iterative version with a stack as well
/// https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/625/discuss/32112/Learn-one-iterative-inorder-traversal-apply-it-to-multiple-tree-questions-(Java-Solution)
[TestClass]
public class IsValidBSTtest
{
// 2
// / \
// 1 3
[TestMethod]
public void ValidTree()
{
TreeNode root = new TreeNode(2);
root.left = new TreeNode(1);
root.right = new TreeNode(3);
IsValidBSTclass temp = new IsValidBSTclass();
Assert.IsTrue(temp.IsValidBST(root));
}
[TestMethod]
public void NotValidTree()
{
TreeNode root = new TreeNode(5);
root.left = new TreeNode(1);
root.right = new TreeNode(4);
root.right.left = new TreeNode(3);
root.right.right = new TreeNode(6);
IsValidBSTclass temp = new IsValidBSTclass();
Assert.IsFalse(temp.IsValidBST(root));
}
/*
* 10
* / \
* 5 15
* /\
* 6 20
*/
[TestMethod]
public void NotValidTree2()
{
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(20);
IsValidBSTclass temp = new IsValidBSTclass();
Assert.IsFalse(temp.IsValidBST(root));
}
}
public class IsValidBSTclass
{
public bool IsValidBST(TreeNode root)
{
return Helper(root, null, null);
}
public bool Helper(TreeNode root, TreeNode minNode, TreeNode maxNode)
{
if (root == null)
{
return true;
}
if (minNode != null && root.val <= minNode.val || maxNode != null && root.val >= maxNode.val)
{
return false;
}
return Helper(root.left, minNode, root) && Helper(root.right, root, maxNode);
}
}
}