class Solution
{
public static int sumOfLeafNodes(Node root)
{
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return root.data;
}
return sumOfLeafNodes(root.left) + sumOfLeafNodes(root.right);
}
}
int sumOfLeafNodes(Node* root) {
if (root == nullptr)
return 0;
if (root->left == nullptr && root->right == nullptr)
return root->data;
return sumOfLeafNodes(root->left) + sumOfLeafNodes(root->right);
}
class Solution:
def sumOfLeafNodes(self, root):
def dfs(node):
nonlocal soln
if not node:
return
if not node.left and not node.right:
soln += node.data
dfs(node.left)
dfs(node.right)
soln = 0
dfs(root)
return soln