Skip to content

Instantly share code, notes, and snippets.

@madhur
Created May 25, 2019 13:05
Show Gist options
  • Select an option

  • Save madhur/0281011fdd676707e952b23c73f8f7cd to your computer and use it in GitHub Desktop.

Select an option

Save madhur/0281011fdd676707e952b23c73f8f7cd to your computer and use it in GitHub Desktop.
Path length from root
public int pathLengthFromRoot(TreeNode root, int n1) {
if (root == null) return 0;
else {
int out = 0;
if ((root.data == n1) || (out = pathLengthFromRoot(root.left, n1)) > 0
|| (out = pathLengthFromRoot(root.right, n1)) > 0) {
return out + 1;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment