Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void traverse (Node root){ // Each child of a tree is a root of its subtree.
- if (root.left != null){
- traverse (root.left);
- }
- System.out.println(root.data);
- if (root.right != null){
- traverse (root.right);
- }
- }
- public Node traverse (Node root, int data){ // What data are you looking for again?
- if(root.data == data) {
- return root;
- }
- if (root.left != null && data < root.data) {
- return traverse (root.left, data);
- }
- if (root.right != null && data > root.data) {
- return traverse (root.right, data);
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement