Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The wording is a little confusing, but I think you mean the maximum of</p> <ul> <li>the maximum length of a path that starts at any node and then only goes to the left, or</li> <li>the maximum length of a path that starts at any node and then only goes to the right.</li> </ul> <p>You do this in two passes, one to find the max left path and one to find the max right path (and then take the max of those two). Or you can do it in a single pass that does both at once.</p> <p>For every node, you want to know three values:</p> <ol> <li>the length of the left path starting at that node,</li> <li>the length of the right path starting at that node, and</li> <li>the length of the longest path starting at that node or one of its descendants.</li> </ol> <p>If you are doing this recursively, this means the recursion should return these three values, probably as a small array or as a simple three-field object.</p> <p>This would look something like</p> <pre><code>Results calculate(Tree node) { if (node == null) return new Results(0,0,0); else { Results leftResults = calculate(node.left); Results rightResults = calculate(node.right); int leftLength = 1 + leftResults.leftLength; int rightLength = 1 + rightResults.rightLength; int maxLength = Math.max(Math.max(leftLength, rightLength), Math.max(leftResults.maxLength, rightResults.maxLength)); return new Results(leftLength, rightLength, maxLength); } } </code></pre> <p>and the overall result would just be <code>calculate(root).maxLength</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload