public bool FindTarget(TreeNode root, int k) { BSTIterator nextIterator = new BSTIterator(root), preIterator = new BSTIterator(root, true); for (TreeNode left = nextIterator.HasNext() ? nextIterator.Next() : null, right = preIterator.HasNext()? preIterator.Next() : null; left.val < right.val;) { int sum = left.val + right.val; if (sum == k) { return(true); } if (sum < k) { left = nextIterator.HasNext()? nextIterator.Next() : null; } else { right = preIterator.HasNext()? preIterator.Next() : null; } } return(false); }
public void Test1() { BSTIterator iterator = new BSTIterator(_root); var n1 = iterator.Next(); // return 3 var n2 = iterator.Next(); // return 7 var hasNext1 = iterator.HasNext(); // return true var n3 = iterator.Next(); // return 9 var hasNext2 = iterator.HasNext(); // return true var n4 = iterator.Next(); // return 15 var hasNext3 = iterator.HasNext(); // return true var n5 = iterator.Next(); // return 20 var hasNext4 = iterator.HasNext(); // return false Assert.Pass(); }
public void HasNextTest() { TreeNode root = new TreeNode(2); root.left = new TreeNode(1); root.right = new TreeNode(3); BSTIterator bst = new BSTIterator(root); Assert.IsTrue(bst.HasNext()); Assert.IsTrue(bst.Next() == 1); Assert.IsTrue(bst.HasNext()); Assert.IsTrue(bst.Next() == 2); Assert.IsTrue(bst.HasNext()); Assert.IsTrue(bst.Next() == 3); Assert.IsFalse(bst.HasNext()); }
public static bool Run(TreeNode p, TreeNode q) { if (p == null || q == null) { return(false); } var pIt = new BSTIterator(p); var qIt = new BSTIterator(q); while (pIt.HasNext() && qIt.HasNext()) { if (pIt.Next() != qIt.Next()) { return(false); } } return(!pIt.HasNext() && !qIt.HasNext()); }
public void Run() { var iterator = new BSTIterator(Utility.ConvertToTreeNode("[7,3,15,null,null,9,20]")); iterator.Next(); iterator.Next(); iterator.Next(); iterator.Next(); iterator.Next(); iterator.HasNext(); }
public void Case1() { var input = new TreeNode(7); input.left = new TreeNode(3); input.right = new TreeNode(15); input.right.left = new TreeNode(9); input.right.right = new TreeNode(20); var target = new BSTIterator(input); Assert.Equal(3, target.Next()); Assert.Equal(7, target.Next()); Assert.True(target.HasNext()); Assert.Equal(9, target.Next()); Assert.True(target.HasNext()); Assert.Equal(15, target.Next()); Assert.True(target.HasNext()); Assert.Equal(20, target.Next()); Assert.False(target.HasNext()); }
private void Check(TreeNode root) { var it = new BSTIterator(root); var v = 0; while (it.HasNext()) { var n = it.Next(); Assert.True(v < n); v = n; } }
public void DataStructure_BSTIteratorTest1() { TreeNode root = new TreeNode(4), l = new TreeNode(2), r = new TreeNode(5), ll = new TreeNode(1), lr = new TreeNode(3); root.Left = l; root.Right = r; l.Left = ll; l.Right = lr; BSTIterator it = new BSTIterator(root); Assert.IsTrue(it.HasNext()); Assert.AreEqual(1, it.Next()); Assert.AreEqual(2, it.Next()); Assert.AreEqual(3, it.Next()); Assert.AreEqual(4, it.Next()); Assert.AreEqual(5, it.Next()); Assert.IsFalse(it.HasNext()); }
public static void Run() { var st = new Solution173(); Console.WriteLine("Start: {0}", DateTime.Now); TreeNode root = new TreeNode(10); root.left = new TreeNode(5); root.right = new TreeNode(20); root.left.left = new TreeNode(1); root.left.right = new TreeNode(8); root.left.right.left = new TreeNode(7); root.right.right = new TreeNode(21); BSTIterator i = new BSTIterator(root); while (i.HasNext()) Console.WriteLine(i.Next()); Console.WriteLine("End: {0}", DateTime.Now); }