Advertisement
Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. public void traverse (Node root){ // Each child of a tree is a root of its subtree.
  2.     if (root.left != null){
  3.         traverse (root.left);
  4.     }
  5.     System.out.println(root.data);
  6.     if (root.right != null){
  7.         traverse (root.right);
  8.     }
  9. }
  10.  
  11. public Node traverse (Node root, int data){ // What data are you looking for again?
  12.     if(root.data == data) {
  13.         return root;
  14.     }
  15.     if (root.left != null && data < root.data) {
  16.         return traverse (root.left, data);
  17.     }
  18.     if (root.right != null && data > root.data) {
  19.         return traverse (root.right, data);
  20.     }
  21.     return null;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement