<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-23965912372324391</id><updated>2011-04-21T13:01:14.383-07:00</updated><category term='Misc'/><category term='Project Euler'/><category term='Binary Tree'/><category term='Analysis'/><title type='text'>Learning To Code...</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://learnercoder.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://learnercoder.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>YC</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-23965912372324391.post-5094988308404668610</id><published>2008-11-16T02:44:00.001-08:00</published><updated>2008-11-16T02:53:18.522-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Project Euler</title><content type='html'>&lt;p&gt;Hi all, it's been a while since I've updated, since I've been quite busy. Anyway, I found out about &lt;a href="http://projecteuler.net/" target="_blank"&gt;Project Euler&lt;/a&gt; through &lt;a href="http://lifehacker.com/5080232/project-euler-exercises-your-mind-with-mathematical-problems" target="_blank"&gt;Lifehacker&lt;/a&gt;, and it's really challenging and fun. Taken from Project Euler's About page:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;It's quite good for de-stressing and taking your mind off certain things. So far, I've managed to solve questions 1, 2, 4, 5, 6 and 9. I'm using Java to solve them, and you're definitely free to use whatever you like. Let's take a look at the problems and solutions:&lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Project Euler Question 1:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&lt;/p&gt;  &lt;p&gt;Find the sum of all the multiples of 3 or 5 below 1000.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;  &lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Question1 {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; belowNum = 1000;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; total = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 1000; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (i % 3 == 0 || i % 5 == 0) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                total += i;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        System.out.println(total);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;strong&gt;&lt;u&gt;&lt;br /&gt;Project Euler Question 2:&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:&lt;br /&gt;&lt;/p&gt;&lt;p&gt;1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Find the sum of all the even-valued terms in the sequence which do not exceed four million.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Question2 {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; one = 1;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; two = 2;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; temp = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; total = 0;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt; (two &amp;lt; 4000000) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;               &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (two % 2 == 0)&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                    total += two;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;      &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            temp = one + two;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            one = two;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            two = temp;              &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        System.out.println(total);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;strong&gt;&lt;u&gt;&lt;br /&gt;Project Euler Question 4:&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.&lt;/p&gt;&lt;p&gt;Find the largest palindrome made from the product of two 3-digit numbers.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Question4 {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; sum = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; result = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 100; i &amp;lt;= 999; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; j = 100; j &amp;lt;= 999; j++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                sum = i * j;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;          &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (isPalindrome(sum) &amp;amp;&amp;amp; sum &amp;gt; result) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;                    result = sum;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        System.out.println(result);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;boolean&lt;/span&gt; isPalindrome(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; s) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        String sum = Integer.toString(s);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        StringBuffer reverseString = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; StringBuffer(sum);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        reverseString.reverse();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        String temp = reverseString.toString();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (sum.equals(temp)) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;strong&gt;&lt;u&gt;&lt;br /&gt;Project Euler Question 5:&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;What is the smallest number that is &lt;dfn&gt;evenly divisible&lt;/dfn&gt; by all of the numbers from 1 to 20?&lt;/p&gt;&lt;span style="font-weight: bold;"&gt;Solution:&lt;/span&gt;&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Question5 {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Elminating some numbers, example: 14 = 7 x 2, we can thus eliminate&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// both 7 and 2. We'll be left with:&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// 11, 12, 13, 14, 15, 16, 17, 18, 19, 20&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Something tell me I can eliminate 12 and 15 too, but I can't&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// really describe why, so we'll just leave it at that.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Start at 20 since. For a number to be divisible by 20, it has to be a multiple of&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// 20. Thus, we can increment by 20;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; start = 20;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;boolean&lt;/span&gt; found = &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt; &amp;amp;&amp;amp; !found) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            found = &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;; &lt;span style="color: rgb(0, 128, 0);"&gt;//assume it's found&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;      &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 11; i &amp;lt;= 20; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (start % i != 0) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                    found = &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;                    &lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;      &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (found) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                System.out.println(start);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;      &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            start += 20;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;      &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        }  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;          &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;strong&gt;&lt;u&gt;&lt;br /&gt;Project Euler Question 6:&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;The sum of the squares of the first ten natural numbers is,&lt;br /&gt;&lt;/p&gt;&lt;p&gt;1&lt;sup&gt;2&lt;/sup&gt; + 2&lt;sup&gt;2&lt;/sup&gt; + ... + 10&lt;sup&gt;2&lt;/sup&gt; = 385&lt;br /&gt;&lt;/p&gt;&lt;p&gt;The square of the sum of the first ten natural numbers is,&lt;/p&gt;&lt;p&gt;(1 + 2 + ... + 10)&lt;sup&gt;2&lt;/sup&gt; = 55&lt;sup&gt;2&lt;/sup&gt; = 3025&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.&lt;/p&gt;&lt;p&gt;Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Question6 {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; sumOfSquare = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; sum = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 1; i &amp;lt;= 100; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            sum += i;      &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;            sumOfSquare += (i*i);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        System.out.println((sum*sum)-sumOfSquare);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;u&gt;Project Euler Question 9:&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;A Pythagorean triplet is a set of three natural numbers, &lt;var&gt;a&lt;/var&gt; &lt; &lt;var&gt;b&lt;/var&gt; &lt; &lt;var&gt;c&lt;/var&gt;, for which,&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;var&gt;a&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;var&gt;b&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = &lt;var&gt;c&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;For example, 3&lt;sup&gt;2&lt;/sup&gt; + 4&lt;sup&gt;2&lt;/sup&gt; = 9 + 16 = 25 = 5&lt;sup&gt;2&lt;/sup&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;There exists exactly one Pythagorean triplet for which &lt;var&gt;a&lt;/var&gt; + &lt;var&gt;b&lt;/var&gt; + &lt;var&gt;c&lt;/var&gt; = 1000.&lt;br /&gt;&lt;br /&gt;Find the product &lt;var&gt;abc&lt;/var&gt;.&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Solution:&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Question9 {  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// a^2 + b^2 = c^2&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// a + b + c = 1000&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Therefore, c = 1000 - a - b&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// a^2 + b^2 = (1000 - a - b)^2&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Simplifying:&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// b = (500000 - 1000a) / (1000 - a)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Therefore, we got to find a values of a and b that satisfy the above.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Since total is 1000, a and b must also be less than 1000. Also (1000 - a),&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// Meaning, if a is 1000, there will be a divide-by-zero. Secondly,&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// 500000 - 1000a, thus I assume that a cannot exceed 500.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; b = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; c = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; a = 1; a &amp;lt; 500; a++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            b = (500000 - 1000*a) / (1000 - a);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;            c = 1000 - a - b;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                              &lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (a*a + b*b == c*c) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                System.out.println("&lt;span style="color: rgb(139, 0, 0);"&gt;a: &lt;/span&gt;" + a);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                System.out.println("&lt;span style="color: rgb(139, 0, 0);"&gt;b: &lt;/span&gt;" + b);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                System.out.println("&lt;span style="color: rgb(139, 0, 0);"&gt;c: &lt;/span&gt;" + c);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; product = a*b*c;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                System.out.println("&lt;span style="color: rgb(139, 0, 0);"&gt;Product: &lt;/span&gt;" + product);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;        } &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;Alright. That's about it. The cool thing about Project Euler is that for some of them, after solving the questions you get access to a PDF file that gives you ideas and MUCH more elegant solutions than what I've done. Also, you get to see the forums for that particular question, and some of the solutions are really, really elegant. It's just too bad that my understanding of Mathematics has yet to reach that level, but it's always good to have a goal =)! On another note, for those of you who are interested, but have no programming background, you may want to try out &lt;a href="http://lifehacker.com/5081669/small-basic-teaches-programming-fundamentals" target="_blank"&gt;Small Basic&lt;/a&gt; (link to Lifehacker.com, again). Personally, I'd suggest starting out with &lt;a href="http://java.sun.com/" target="_blank"&gt;Java&lt;/a&gt;, but well, it's always good to have choices!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/23965912372324391-5094988308404668610?l=learnercoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnercoder.blogspot.com/feeds/5094988308404668610/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=23965912372324391&amp;postID=5094988308404668610' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/5094988308404668610'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/5094988308404668610'/><link rel='alternate' type='text/html' href='http://learnercoder.blogspot.com/2008/11/project-euler.html' title='Project Euler'/><author><name>YC</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-23965912372324391.post-5950031998981052613</id><published>2008-11-02T00:02:00.001-07:00</published><updated>2008-11-02T09:36:23.133-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Analysis'/><title type='text'>Analyzing Problems *Properly*</title><content type='html'>I'm writing this as a summary of lessons learnt (through the hard way) since I started studying this year. Some of you might have figured out by now, I'm currently taking CS1102 (Data Structures &amp;amp; Algorithms). It's a pretty good module, and I really enjoy it. However, when it really mattered, I always seem to screw up. I had my Practical Examination yesterday, which consisted of 2 parts, one worth 30%, and the other worth 70%.&lt;br /&gt;&lt;br /&gt;For the 30% part, we had the following (something similar, can't remember the exact thing):&lt;br /&gt;&lt;br /&gt;Character Sequence:&lt;br /&gt;AXXAIOUAAXXUOIAXAXIOUAAXXAIOU&lt;br /&gt;&lt;br /&gt;Test Cases:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;AXXAIOU&lt;/li&gt;&lt;br /&gt;&lt;li&gt;AIOUAAX&lt;/li&gt;&lt;br /&gt;&lt;li&gt;AAXXUOI&lt;/li&gt;&lt;br /&gt;&lt;li&gt;AXAIOUA&lt;/li&gt;&lt;br /&gt;&lt;li&gt;AAAXXXX&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;I can't remember the exact scenario, but basically the input consisted of the above character sequence, and the test cases. The test cases will always be in groups of 7 chracters. You are supposed to output the position of the sequence if found, which in the above case, you should output:&lt;br /&gt;&lt;br /&gt;0, 22&lt;br /&gt;3&lt;br /&gt;7&lt;br /&gt;14&lt;br /&gt;Not found&lt;br /&gt;&lt;br /&gt;Moreover, the question wants you to be able to query in less than O(n) time.&lt;br /&gt;&lt;br /&gt;Explanation: AXXAIOU is found at position 0 and 22. AIOUAAX is found at position 3. AAXXUOI is found at position 7. AXAIOUA is found at position 14. Finally, AAAXXXX is not found in the sequence.&lt;br /&gt;&lt;br /&gt;&lt;table width="599" cellpadding="2" cellspacing="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="16"&gt;A&lt;/td&gt;        &lt;td width="16" align="center"&gt;X&lt;/td&gt;        &lt;td valign="top" width="16"&gt;X&lt;/td&gt;        &lt;td valign="top" width="16"&gt;A&lt;/td&gt;        &lt;td valign="top" width="15"&gt;I&lt;/td&gt;        &lt;td valign="top" width="17"&gt;O&lt;/td&gt;        &lt;td valign="top" width="16"&gt;U&lt;/td&gt;        &lt;td valign="top" width="16"&gt;A&lt;/td&gt;        &lt;td valign="top" width="16"&gt;A&lt;/td&gt;        &lt;td valign="top" width="16"&gt;X&lt;/td&gt;        &lt;td valign="top" width="23"&gt;X&lt;/td&gt;        &lt;td valign="top" width="23"&gt;U&lt;/td&gt;        &lt;td valign="top" width="23"&gt;O&lt;/td&gt;        &lt;td valign="top" width="23"&gt;I&lt;/td&gt;        &lt;td valign="top" width="23"&gt;A&lt;/td&gt;        &lt;td valign="top" width="23"&gt;X&lt;/td&gt;        &lt;td valign="top" width="23"&gt;A&lt;/td&gt;        &lt;td valign="top" width="23"&gt;X&lt;/td&gt;        &lt;td valign="top" width="23"&gt;I&lt;/td&gt;        &lt;td valign="top" width="23"&gt;O&lt;/td&gt;        &lt;td valign="top" width="23"&gt;U&lt;/td&gt;        &lt;td valign="top" width="23"&gt;A&lt;/td&gt;        &lt;td valign="top" width="23"&gt;A&lt;/td&gt;        &lt;td valign="top" width="23"&gt;X&lt;/td&gt;        &lt;td valign="top" width="23"&gt;X&lt;/td&gt;        &lt;td valign="top" width="23"&gt;A&lt;/td&gt;        &lt;td valign="top" width="23"&gt;I&lt;/td&gt;        &lt;td valign="top" width="23"&gt;O&lt;/td&gt;        &lt;td valign="top" width="23"&gt;U&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="16"&gt;0&lt;/td&gt;        &lt;td valign="top" width="16"&gt;1&lt;/td&gt;        &lt;td valign="top" width="16"&gt;2&lt;/td&gt;        &lt;td valign="top" width="16"&gt;3&lt;/td&gt;        &lt;td valign="top" width="15"&gt;4&lt;/td&gt;        &lt;td valign="top" width="17"&gt;5&lt;/td&gt;        &lt;td valign="top" width="16"&gt;6&lt;/td&gt;        &lt;td valign="top" width="16"&gt;7&lt;/td&gt;        &lt;td valign="top" width="16"&gt;8&lt;/td&gt;        &lt;td valign="top" width="16"&gt;9&lt;/td&gt;        &lt;td valign="top" width="23"&gt;10&lt;/td&gt;        &lt;td valign="top" width="23"&gt;11&lt;/td&gt;        &lt;td valign="top" width="23"&gt;12&lt;/td&gt;        &lt;td valign="top" width="23"&gt;13&lt;/td&gt;        &lt;td valign="top" width="23"&gt;14&lt;/td&gt;        &lt;td valign="top" width="23"&gt;15&lt;/td&gt;        &lt;td valign="top" width="23"&gt;16&lt;/td&gt;        &lt;td valign="top" width="23"&gt;17&lt;/td&gt;        &lt;td valign="top" width="23"&gt;18&lt;/td&gt;        &lt;td valign="top" width="23"&gt;19&lt;/td&gt;        &lt;td valign="top" width="23"&gt;20&lt;/td&gt;        &lt;td valign="top" width="23"&gt;21&lt;/td&gt;        &lt;td valign="top" width="23"&gt;22&lt;/td&gt;        &lt;td valign="top" width="23"&gt;23&lt;/td&gt;        &lt;td valign="top" width="23"&gt;24&lt;/td&gt;        &lt;td valign="top" width="23"&gt;25&lt;/td&gt;        &lt;td valign="top" width="23"&gt;26&lt;/td&gt;        &lt;td valign="top" width="23"&gt;27&lt;/td&gt;        &lt;td valign="top" width="23"&gt;28&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;Analysis: Based on the given example, I deduced that overlapping cases can occur, and that each sequence can occur more than once. So what did I do wrongly? I did not read the question thoroughly enough to know that only the query (test cases) needs to be less than O(n) time. I thought the entire program should not take O(n) time. So I proceeded to implement some methods using String, and still ended up with O(n) time. The truth is, since the character sequence portion does not have to be less than O(n) time, I can simply store it into a hash table as follows:&lt;br /&gt;&lt;br /&gt;AXXAIOU&lt;br /&gt;XXAIOUA&lt;br /&gt;XAIOUAA&lt;br /&gt;&lt;br /&gt;And so on. That is, I can simply substring(i, 7) as I iterate through the character sequence, and put it into a Hashtable, all the way till I hit length of sequence - 7, which would give me the last substring of:&lt;br /&gt;&lt;br /&gt;AXXAIOU&lt;br /&gt;&lt;br /&gt;Once everything is inserted into the Hashtable, querying/retrieving it is O(1) time. I have no idea why did I not read the question properly. Sigh. I'll probably still pass, but not get the full marks.&lt;/p&gt;  &lt;p&gt;Next, we have the 70% part, which I came SO CLOSE to finishing it, I just UNDER-analyzed it. Basically, suppose you have the following:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    7&lt;br /&gt;   / \&lt;br /&gt;  5   4&lt;br /&gt; /   / \&lt;br /&gt;6   1   3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You are then supposed to calculate the largest number based on the following:&lt;br /&gt;&lt;br /&gt;n1 + (n2 - (n3 + (n4 - (n5 + n6)))...&lt;br /&gt;&lt;p&gt;Meaning, using the above example, there are 3 paths:&lt;/p&gt;&lt;p&gt;7 + (5 - 6) = 6&lt;br /&gt;7 + (4 - 1) = 10&lt;br /&gt;7 + (4 - 3) = 8&lt;/p&gt;Thus, the longest path is 10 in this case. The input you are given is:&lt;br /&gt;&lt;br /&gt;7 2 3&lt;br /&gt;5 1 0&lt;br /&gt;6 0 0&lt;br /&gt;4 1 1&lt;br /&gt;1 0 0&lt;br /&gt;3 0 0&lt;br /&gt;&lt;br /&gt;Which means, to the left of 7, there are 2 nodes, and to the right of 7, there are 3 nodes. To the left of 5, there is one node, and none to the right. 6 has no child nodes. 4 has one child each on the left and right. Both 1 and 3 have no child nodes. Basically, the first digit of each line is the actual node value, given in pre-order. The second digit means the number of left child nodes, and the third digit means the number of right child nodes. Thus, you have to build the tree out, and then find the longest path.&lt;br /&gt;&lt;br /&gt;Analysis: Basically, this problem can be broken up into 2 parts, building the tree, and then finding the longest path. I was able to build the tree relatively quickly. What I did was to have something like the following:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; buildTree(ArrayList&amp;lt;TempNode&amp;gt; nodes) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    root = buildTree(nodes, 0);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;Node buildTree(ArrayList&amp;lt;TempNode&amp;gt; nodes, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; curr) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    Node temp = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (curr &amp;lt; nodes.size()) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        temp = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Node(nodes.get(curr));&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (nodes.get(curr).left != 0)&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            temp.left = buildTree(nodes, curr+1);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (nodes.get(curr).right != 0)&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            temp.right = buildTree(nodes, curr+1+nodes.get(curr).right);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; node;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; TempNode {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; value, left, right;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    TempNode(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; value, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; left, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; right) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.value = value;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.left = left;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.right = right;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;What I did was to store each line of input into a TempNode object, and all of that is then stored into an ArrayList. The tree is then built recursively, and if you study the input (since it's pre-order), you will realise that the node to the right of the current node is always the position of the current pointer + the number of left nodes of the current node + 1. Take note that the above code might be missing a things because I don't exactly remember everything, but the general concept is that. Thus, I was able to build out the tree. Now comes the longest path part. I made use of a concept similar to finding the height of the tree, which is as follows:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; findHeightOfTree(Node node) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; 1 + Math.max(findHeightOfTree(node.left), findHeightOfTree(node.right));&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;}&lt;/pre&gt;&lt;/pre&gt;What the code does is that, starting at the node you specify (the root node in this case), it will traverse left and right and return the maximum value at that point in time. Thus, if I were to change the code to the following:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; findMaxValue(Node node) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; node.value + Math.max(findMaxValue(node.left), findMaxValue(node.right));&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;I would be able to find the maximum value, PROVIDED all the values are added together. Meaning that the above tree would give:&lt;br /&gt;&lt;br /&gt;7 + (5 + 6) = 18&lt;br /&gt;7 + (4 + 1) = 12&lt;br /&gt;7 + (4 + 3) = 14&lt;br /&gt;&lt;br /&gt;Which is very similar to what the question wants. So I thought, now I just need to add in a boolean to alternate + and -. And the code became:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; findMaxValue(Node node, &lt;span style="color: rgb(0, 0, 255);"&gt;boolean&lt;/span&gt; plus) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (plus)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; node.value + Math.max(findMaxValue(node.left, !plus), findMaxValue(node.right, !plus));&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; node.value - Math.max(findMaxValue(node.left, !plus), findMaxValue(node.right, !plus));&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;Sadly, it did not work as expected, and I had 30 minutes before the end of the practical. I finally figured out at the 29th minute what were the mistakes, but before I could magically try to code it in 1 min, I had to press some key wrongly in Vim that resulted in it being unable to respond to me (I'm quite new to Vim, and I still don't know what I pressed). Thus, I was REALLY close, but not close enough. Here is what I realised, but could not put into code:&lt;br /&gt;&lt;p&gt;Firstly, I had to return the maximum value at the node itself, which means I should have the following instead:&lt;br /&gt;&lt;/p&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; findMaxValue(Node node, &lt;span style="color: rgb(0, 0, 255);"&gt;boolean&lt;/span&gt; plus) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (plus)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; Math.max(node.value + findMaxValue(node.left, !plus), node.value + findMaxValue(node.right, !plus));&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; Math.max(node.value - findMaxValue(node.left, !plus), node.value - findMaxValue(node.right, !plus));&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;}&lt;/pre&gt;&lt;/pre&gt;I managed to do this part, thankfully. Of course, the output was still wrong. So what did I missed out (which I realised LITERALLY at the last minute)? Look at the following:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    7&lt;br /&gt;   / \&lt;br /&gt;  5   4&lt;br /&gt; /   / \&lt;br /&gt;6   1   3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Look at Node 5. I was doing a comparison between 5-6, and 5-0 (since 5 has no left child, the code will return 0). Of course, the code will return 5-0 as the maximum value, which is 5. That is what I missed out. I neglected the MINUS case, whereby if a node has either no right or left child, there should be no comparison taking place. For the case of plus, it is ok, because 5+6 is still more than 5+0. Alas, it was too late, and I could not complete what I SHOULD HAVE completed. Sadly =(. I haven't tried actually compiling or writing the whole thing out, but the final correct code should have been something like:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 600px; background-color: rgb(251, 251, 251);"&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; findMaxValue(Node node, &lt;span style="color: rgb(0, 0, 255);"&gt;boolean&lt;/span&gt; plus) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (plus)&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; Math.max(node.value + findMaxValue(node.left, !plus), node.value + findMaxValue(node.right, !plus));&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node.left == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; node.right == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node.left == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                    &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; node.value - findMaxValue(node.right);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                    &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; node.value - findMaxValue(node.left);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;                }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(255, 255, 255);"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; width: 100%; background-color: rgb(251, 251, 251);"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; Math.max(node.value - findMaxValue(node.left, !plus), node.value - findMaxValue(node.right, !plus));&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;}&lt;/pre&gt;&lt;/pre&gt;There you have it. I under-analyzed the problem, and neglected one case, and could not get the correct output. Sigh. To be honest, I'm still not quite over it, but I've learnt my lesson, and I think that is important in itself. I just don't understand, why, for 29 minutes, I thought of every possible scenario I could have gone wrong, but this. This simple case, of not taking care of a possible empty child node. Why =(?&lt;br /&gt;&lt;br /&gt;Lesson Learnt: Analyze your problems properly. Use a highlighter if necessary. Sometimes luck REALLY isn't on your side. Sigh.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/23965912372324391-5950031998981052613?l=learnercoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnercoder.blogspot.com/feeds/5950031998981052613/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=23965912372324391&amp;postID=5950031998981052613' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/5950031998981052613'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/5950031998981052613'/><link rel='alternate' type='text/html' href='http://learnercoder.blogspot.com/2008/11/analyzing-problems-properly.html' title='Analyzing Problems *Properly*'/><author><name>YC</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-23965912372324391.post-418540124665192184</id><published>2008-11-01T11:52:00.001-07:00</published><updated>2008-11-02T09:10:05.096-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Binary Tree'/><title type='text'>Building A Binary Tree (From Post/Pre-Order &amp; In-Order Traversal)</title><content type='html'>&lt;p&gt;As most of you know, it is possible to build a Binary Tree from either its post-order/pre-order traversal and its in-order traversal. For example, given the following:&lt;/p&gt;  &lt;p&gt;Pre-Order: 1 2 4 5 8 9 3 6 0 7&lt;br /&gt;In-Order: 4 2 8 5 9 1 6 0 3 7&lt;/p&gt;  &lt;p&gt;You can re-construct the Binary Tree and get the following:&lt;br /&gt;&lt;/p&gt;&lt;pre&gt;&lt;br /&gt;       1&lt;br /&gt;   /       \&lt;br /&gt;  2         3&lt;br /&gt; / \       / \&lt;br /&gt;4   5     6   7&lt;br /&gt;   / \     \&lt;br /&gt;  8   9     0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Basically, the pre-order traversal will give you the order in which the nodes should be added, and the in-order traversal will give you the position. For example, using the pre-order example, we have "1". Since it's pre-order, which means that it's traversed in the order Root-Left-Right, "1" would be the root node since it appears first. Thus, we create "1" accordingly. Take note of the position of "1" in the in-order traversal. Next, we have "2" (pre-order), which means it's the next node to be created/inserted. Looking at the in-order traversal, we see that "2" is to the left of "1", and thus, "2" would be the left node of "1". We now look at "3" (pre-order), and see that it is to the right of "1" in in-order, and thus add "3" to the left of "1" accordingly.&lt;br /&gt;&lt;br /&gt;We now look at "4" (pre-order), and see that it is to the left of "2" (in-order). Next up, is "5", which is to the right of "2". This goes on until the whole tree is created. I figured that since it is quite easy to figure out once you get the concept, why not put it into code? It can be seen that this can be coded recursively, and so I went ahead and implemented it in Java. However, I ran into some problems with the base case, and after doing some search online, I found &lt;a title="http://www.dotnetjunkies.com/WebLog/debasish/archive/2006/05/31/139364.aspx" href="http://www.dotnetjunkies.com/WebLog/debasish/archive/2006/05/31/139364.aspx"&gt;http://www.dotnetjunkies.com/WebLog/debasish/archive/2006/05/31/139364.aspx&lt;/a&gt;, which gave me the solution to the base case I want. Ok, enough talking, and here's the code:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; width: 580px; min-height: 40px; background-color: rgb(251, 251, 251);"&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/*&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Author: Soh Yuan Chin&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Explanation:&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Create a tree from its preorder/postorder and inorder&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * The first line of input will have 0 or 1. 0 = preorder. 1 = postorder.&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Second line of input determines number of nodes.&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Third line of input contains the pre/post order.&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Fourth line of input contains the in order.&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; *&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * Example Input:&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * 0&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * 10&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * 1 2 4 5 8 9 3 6 0 7&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; * 4 2 8 5 9 1 6 0 3 7&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; *&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt; */&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;import&lt;/span&gt; java.util.*;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; BuildBinaryTree {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; main(String[] args) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        Scanner sc = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Scanner(System.in);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        BinaryTree bt = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; BinaryTree();&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; type = sc.nextInt();&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; numOfNodes = sc.nextInt();&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] preOrPost = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[numOfNodes];&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] inOrder = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[numOfNodes];&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; numOfNodes; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            preOrPost[i] = sc.nextInt();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; numOfNodes; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            inOrder[i] = sc.nextInt();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        bt.buildTree(preOrPost, inOrder, type);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        bt.preOrder();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        bt.inOrder();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        bt.postOrder();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; TreeNode {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; number;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; TreeNode left;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; TreeNode right;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    TreeNode(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; number) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.number = number;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; BinaryTree {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; TreeNode root;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; size;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    BinaryTree() {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        root = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        size = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;// Building Tree    &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; buildTree(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] preOrPost, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] inOrder, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; type) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (type == 0)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            root = buildPreOrderTree(preOrPost, inOrder, 0, 0, preOrPost.length);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (type == 1) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 128, 0);"&gt;// reverse the array. exchange the first and last.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; left=0, right=preOrPost.length-1; left &amp;lt; right; left++, right--) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; temp = preOrPost[left];&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;                preOrPost[left] = preOrPost[right];&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;                preOrPost[right] = temp;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            root = buildPostOrderTree(preOrPost, inOrder, 0, 0, preOrPost.length);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; TreeNode buildPostOrderTree(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] preOrPost, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] inOrder, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; curr, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; start, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; end) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        TreeNode currNode = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (end-start == 1) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;            currNode = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TreeNode(inOrder[start]);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; k = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = start; i &amp;lt; end; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (preOrPost[curr] == inOrder[i]) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                    k = i;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                    &lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (k &amp;gt; 0) {&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                currNode = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TreeNode(inOrder[k]);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;                currNode.right = buildPostOrderTree(preOrPost, inOrder, curr+1, k+1, end);&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;                currNode.left = buildPostOrderTree(preOrPost, inOrder, curr+(k-start), start, k);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; currNode;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; TreeNode buildPreOrderTree(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] preOrPost, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] inOrder, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; curr, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; start, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; end) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        TreeNode currNode = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre   style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (end-start == 1) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            currNode = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TreeNode(inOrder[start]);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; k = 0;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = start; i &amp;lt; end; i++) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (preOrPost[curr] == inOrder[i]) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;                    k = i;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;                    &lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;                }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (k &amp;gt; 0) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;                currNode = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TreeNode(inOrder[k]);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;                currNode.left = buildPreOrderTree(preOrPost, inOrder, curr+1, start, k);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;                currNode.right = buildPreOrderTree(preOrPost, inOrder, curr+1+(k-start), k+1, end);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; currNode;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;// Traverse&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; preOrder() {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        System.out.print("&lt;span style="color: rgb(139, 0, 0);"&gt;Pre-Order: &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        preOrder(root);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        System.out.println();&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; preOrder(TreeNode node) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            System.out.print(node.number + "&lt;span style="color: rgb(139, 0, 0);"&gt; &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;            preOrder(node.left);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            preOrder(node.right);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; postOrder() {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        System.out.print("&lt;span style="color: rgb(139, 0, 0);"&gt;Post-Order: &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        postOrder(root);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        System.out.println();&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; postOrder(TreeNode node) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            postOrder(node.left);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;            postOrder(node.right);&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            System.out.print(node.number + "&lt;span style="color: rgb(139, 0, 0);"&gt; &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; inOrder() {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        System.out.print("&lt;span style="color: rgb(139, 0, 0);"&gt;In-Order: &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        inOrder(root);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        System.out.println();&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; inOrder(TreeNode node) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            inOrder(node.left);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;            System.out.print(node.number + "&lt;span style="color: rgb(139, 0, 0);"&gt; &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            inOrder(node.right);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; levelOrder() {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        System.out.print("&lt;span style="color: rgb(139, 0, 0);"&gt;Level-Order: &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;        levelOrder(root);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        System.out.println();&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; levelOrder(TreeNode node) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (node != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;/pre&gt;&lt;pre face="consolas,'Courier New',courier,monospace" size="12px" style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%;"&gt;            Queue&amp;lt;TreeNode&amp;gt; tempQ = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; LinkedList&amp;lt;TreeNode&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;            tempQ.offer(node);        &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;            TreeNode temp = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt; (!tempQ.isEmpty()) {&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;                temp = tempQ.poll();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;                System.out.print(temp.number + "&lt;span style="color: rgb(139, 0, 0);"&gt; &lt;/span&gt;");&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (temp.left != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;                    tempQ.offer(temp.left);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (temp.right != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;                    tempQ.offer(temp.right);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;            }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); font-family: consolas,'Courier New',courier,monospace; font-size: 12px; width: 100%;"&gt;&lt;/pre&gt;&lt;/pre&gt;Take note that the above code is able to do both pre-order/post-order and in-order building of tree. Also, you should get an output of printing out the different traversals of the newly-built tree. You can enable level-order traversal by adding the method call to main(), the levelOrder() method has already been implemented. Lastly, I implemented post-order building of tree by reversing the array of elements first. If you do not want to do that, simply modify the values of curr, start, and end accordingly.&lt;br /&gt;&lt;br /&gt;Below are 2 test cases you can try using. Simply save them as a text file, example "test.txt", and use input redirection "java BuildBinaryTree &lt; test.txt".&lt;br /&gt;&lt;br /&gt;Post-Order Test Case:&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;1&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;10&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;4 8 9 5 2 0 6 7 3 1&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;4 2 8 5 9 1 6 0 3 7&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;Pre-Order Test Case:&lt;br /&gt;&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 580px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;0&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;10&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;1 2 4 5 8 9 3 6 0 7&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;4 2 8 5 9 1 6 0 3 7&lt;/pre&gt;&lt;/pre&gt;I hope this has been useful for some of you =)!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/23965912372324391-418540124665192184?l=learnercoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnercoder.blogspot.com/feeds/418540124665192184/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=23965912372324391&amp;postID=418540124665192184' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/418540124665192184'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/418540124665192184'/><link rel='alternate' type='text/html' href='http://learnercoder.blogspot.com/2008/11/building-binary-tree-from-postpre-order.html' title='Building A Binary Tree (From Post/Pre-Order &amp;amp; In-Order Traversal)'/><author><name>YC</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-23965912372324391.post-8115523768095120240</id><published>2008-11-01T11:27:00.001-07:00</published><updated>2008-11-01T11:27:52.974-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Misc'/><title type='text'>First Post</title><content type='html'>&lt;p&gt;I created this blog as a way to share my coding/programming experiences with others, as well as to learn (hopefully there're people commenting). Anyway, a short introduction about myself. I'm currently studying in National University of Singapore (NUS), Year 1, Semester 1, in the School of Computing, and am planning to major in Computer Science.&lt;/p&gt;  &lt;p&gt;Previously, I was from Temasek Polytechnic (TP), and graduated with a Diploma in Computer Engineering. I went on to serve National Service (NS) after that, and started schooling again this year in July. As such, I feel like I'm re-learning programming all over again, and it's been a fun, although tough, journey so far.&lt;/p&gt;  &lt;p&gt;Ok, I guess that's about it.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/23965912372324391-8115523768095120240?l=learnercoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnercoder.blogspot.com/feeds/8115523768095120240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=23965912372324391&amp;postID=8115523768095120240' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/8115523768095120240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/23965912372324391/posts/default/8115523768095120240'/><link rel='alternate' type='text/html' href='http://learnercoder.blogspot.com/2008/11/first-post.html' title='First Post'/><author><name>YC</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
