Pergunta de entrevista da empresa Amazon

Write the code for the preorder traversal of a tree.

Respostas da entrevista

Sigiloso

18 de fev. de 2012

using recursion public static void preorderTraversal(Tree T,Node root){ if(root==null){ return "empty tree"; } else{ visit(T,root); preorderTraversal(T,T,leftChild(root)); preorderTraversal(T,T,rightChild(root)); } }

1

Sigiloso

9 de abr. de 2012

public static void preorderTraversal(TreeNode node) { Stack<div>s = new Stack<div>(); while (node != null || !s.isEmpty()) { while (node != null) { visit(node); node = node.left; } if (!s.isEmpty()) { node = s.pop(); node = node.right; } } }</div></div>

Sigiloso

9 de abr. de 2012

Sorry I forgot to push the node into the stack after visited it

Sigiloso

15 de abr. de 2013

OMG Why my question is to implememt an AVL tree...