We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my Java solution of essentially the same (passes all tests) with code comments:
publicstaticvoidinOrder(Noderoot){Nodecur=root;while(cur!=null){if(cur.left!=null){// Find in-order predecessor of curNodecurPredecessor=cur.left;while(curPredecessor.right!=null&&curPredecessor.right!=cur){curPredecessor=curPredecessor.right;}// curPredecessor now indeed is the predecessor of curif(curPredecessor.right==null){// Thread curPredecessor back to cur (to "remember" it without a stack)curPredecessor.right=cur;// "Recur" to leftcur=cur.left;}else{System.out.print(cur.data+" ");// "Recur" to rightcur=cur.right;}}else{System.out.print(cur.data+" ");// "Recur" to rightcur=cur.right;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Inorder Traversal
You are viewing a single comment's thread. Return to all comments →
Thanks for helping to discover Morris' exists!
Here is my Java solution of essentially the same (passes all tests) with code comments: