Tree Sum of leaves
Execution time limit is 1 second
Runtime memory usage limit is 128 megabytes
Given a binary tree. Find the sum of all its leaves.
Definition of a tree:
// Java class TreeNode { public: int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; left = NULL; right = NULL; };
// C++ class TreeNode { public: int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };
Implement function SumLeaves that returns the sum of values of all its leaves.
// Java int SumLeaves(TreeNode tree)
// C++ int SumLeaves(TreeNode *tree)
Example
Function SumLeaves returns 31 = 5 + 9 + 17.
Submissions 1K
Acceptance rate 45%