HW4B.pdf - 10.4-5 public class TreeNodecfw TreeNode left TreeNode right TreeNode parent int val pulic TreeNode(int val)cfw this.val = val public void

HW4B.pdf - 10.4-5 public class TreeNodecfw TreeNode left...

This preview shows page 1 - 3 out of 7 pages.

10.4-5 public class TreeNode{ TreeNode left; TreeNode right; TreeNode parent; int val; pulic TreeNode(int val){ this.val = val; } } public void traverse(TreeNode root){ TreeNode prev = new TreeNode(0); TreeNode curr = root; while(curr != null){ if( curr.parent == prev ){ System.out.println( curr.val ); prev = curr; if( curr.left != null){ curr = curr.left; } else if(curr.right != null){ curr = curr.right; } else{ curr = curr.parent; } } else if( curr.left = prev ){ prev = curr; if( curr.right != null){ curr = curr.right; } else{ curr = curr.parent; } } else if( curr.right = prev ){ prev = curr; curr = curr.parent; } } }
Image of page 1
2. public class TreeNode{ TreeNode left; TreeNode right; int val; public TreeNode(int val){ this.val = val; } } public boolean isBST(TreeNode root){ if(root == null){ return true; } TreeNode prev = null; Stack<TreeNode> stack = new Stack<>(); while(root != null){ stack.push(root); root = root.left; } while( !stack.isEmpty() ){ TreeNode curr = stack.pop(); if(prev != null && prev.val >= curr.val){ return false; } prev = curr; curr = curr.right; while(curr != null){ stack.push(curr); curr = curr.left; } } return true; } time complexity: the
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 7 pages?

  • Spring '05
  • STALLMANN
  • Algorithms, Computational complexity theory, curr, TreeNode

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes