You are viewing a single comment's thread. Return to all comments →
My java solution. I always find recursive traversals so much easier to conceptualize than stacks, etc.
public static void preOrder(Node root) { String result = preOrderTraverse(root); String cleanedResult = result.trim(); System.out.println(cleanedResult); } public static String preOrderTraverse(Node curNode) { if (curNode==null){ return ""; } else { String result = curNode.data + " " + preOrderTraverse(curNode.left) + preOrderTraverse(curNode.right); return result; } }
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 →
My java solution. I always find recursive traversals so much easier to conceptualize than stacks, etc.