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)The function sumOneSon returns 34 = 4 + 10 + 20.