Tree Sum of nodes with one sun
Easy
Execution time limit is 1 second
Runtime memory usage limit is 128 megabytes
Given a binary search tree. Find the sum of all its nodes with only one child (either left or right).
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 the function sumOneSon that returns the sum of all nodes in the tree that have only one child. If the given tree has no nodes with a single child, return .
// Java int sumOneSon(TreeNode tree) // C++ int sumOneSon(TreeNode *tree)
Note
The function sumOneSon returns .
Submissions 39
Acceptance rate 49%