Skip to main content
fix brainfart / copy/paste error
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

Your recursion is doing far too much, and it can be simplified a lot.

Additionally, you are creating a lot of Data instances, when a simpler mechanism would be to call it a Result<T> and reuse that single node for all values....

private class Result<T> { int depth; T data; } 

Then, as you recurse, you have a single instance of that Result object that you pass to all nodes on the recursion...

private void recurse(Result<T> result, TreeNode<T> node, int depth, boolean isLeft) { if (node == null) { // nothing to do return; } if (isleft && node.left == null && node.right == null) { // we are the left leaf node if (depth > result.depth) { result.depth = depth; result.data = node.item; } } recurse(result, node.left, depth + 1, true); recurse(result, node.leftright, depth + 1, false); } 

This algorithm makes the process a lot cleaner, and makes the recursion more obvious.

Your recursion is doing far too much, and it can be simplified a lot.

Additionally, you are creating a lot of Data instances, when a simpler mechanism would be to call it a Result<T> and reuse that single node for all values....

private class Result<T> { int depth; T data; } 

Then, as you recurse, you have a single instance of that Result object that you pass to all nodes on the recursion...

private void recurse(Result<T> result, TreeNode<T> node, int depth, boolean isLeft) { if (node == null) { // nothing to do return; } if (isleft && node.left == null && node.right == null) { // we are the left leaf node if (depth > result.depth) { result.depth = depth; result.data = node.item; } } recurse(result, node.left, depth + 1, true); recurse(result, node.left, depth + 1, false); } 

This algorithm makes the process a lot cleaner, and makes the recursion more obvious.

Your recursion is doing far too much, and it can be simplified a lot.

Additionally, you are creating a lot of Data instances, when a simpler mechanism would be to call it a Result<T> and reuse that single node for all values....

private class Result<T> { int depth; T data; } 

Then, as you recurse, you have a single instance of that Result object that you pass to all nodes on the recursion...

private void recurse(Result<T> result, TreeNode<T> node, int depth, boolean isLeft) { if (node == null) { // nothing to do return; } if (isleft && node.left == null && node.right == null) { // we are the left leaf node if (depth > result.depth) { result.depth = depth; result.data = node.item; } } recurse(result, node.left, depth + 1, true); recurse(result, node.right, depth + 1, false); } 

This algorithm makes the process a lot cleaner, and makes the recursion more obvious.

Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

Your recursion is doing far too much, and it can be simplified a lot.

Additionally, you are creating a lot of Data instances, when a simpler mechanism would be to call it a Result<T> and reuse that single node for all values....

private class Result<T> { int depth; T data; } 

Then, as you recurse, you have a single instance of that Result object that you pass to all nodes on the recursion...

private void recurse(Result<T> result, TreeNode<T> node, int depth, boolean isLeft) { if (node == null) { // nothing to do return; } if (isleft && node.left == null && node.right == null) { // we are the left leaf node if (depth > result.depth) { result.depth = depth; result.data = node.item; } } recurse(result, node.left, depth + 1, true); recurse(result, node.left, depth + 1, false); } 

This algorithm makes the process a lot cleaner, and makes the recursion more obvious.