MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / findLeafNodes

Method findLeafNodes

java/0129-sum-root-to-leaf-numbers.java:35–52  ·  view source on GitHub ↗
(TreeNode node, int currentPath)

Source from the content-addressed store, hash-verified

33
34 // dfs method
35 public int findLeafNodes(TreeNode node, int currentPath){
36 // base case, if no node then return 0
37 if(node==null){
38 return 0;
39 }
40
41 // add the current node value to the currentPath (move decimal to right by 1 and add)
42 currentPath = (currentPath * 10) + node.val;
43
44 // if we are at a non-null node, check if it is a leaf
45 if(node.left==null && node.right==null){
46 // return the solution
47 return currentPath;
48 }
49
50 // check find the leaf nodes on the left and right
51 return findLeafNodes(node.left, currentPath) + findLeafNodes(node.right, currentPath);
52 }
53}
54
55// solution using strings

Callers 1

sumNumbersMethod · 0.95

Calls 2

toStringMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected