Skip to content

Instantly share code, notes, and snippets.

@micylt
Last active August 24, 2017 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micylt/d7b9cd33ee5abf40a9aa to your computer and use it in GitHub Desktop.
Save micylt/d7b9cd33ee5abf40a9aa to your computer and use it in GitHub Desktop.
returns the number of left children in the tree. A left child is a node that appears as the root of the left-hand subtree of another node. An empty tree has 0 left nodes. For example, the following tree has four left children (the nodes storing the values 5, 1, 4, and 7):
public int countLeftNodes() {
return countLeftNodes(this.overallRoot);
}
private int countLeftNodes(IntTreeNode overallRoot) {
if (overallRoot != null) {
if (overallRoot.left != null) {
return 1 + countLeftNodes(overallRoot.left) + countLeftNodes(overallRoot.right);
} else {
return countLeftNodes(overallRoot.right);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment