You are viewing a single comment's thread. Return to all comments →
java8
Recursive Solution
public static void preOrder(Node root) { if (root == null) return; System.out.print(root.data + " "); preOrder(root.left); preOrder(root.right); }
Non recursive solution
public static void preOrder(Node root) { Stack<Node> stack = new Stack<>(); stack.push(root); while (!stack.empty()) { Node node = stack.pop(); if (node != null) { System.out.print(node.data + " "); stack.push(node.right); stack.push(node.left); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Preorder Traversal
You are viewing a single comment's thread. Return to all comments →
java8
Recursive Solution
Non recursive solution