Given a Binary Tree of size N, your task is to complete the function sumBt(), that should return the sum of all the nodes of the given binary tree.
Input:
First line of input contains the number of test cases T. For each test case, there will be two lines:
	
	First line of each test case will be an integer N denoting the number of parent child relationships.
	
	
	Second line of each test case will print the level order traversal of the tree in the form of N space separated triplets. The description of triplets is as follows:
	
		
		Each triplet will contain three space-separated elements of the form (int, int char).
		
		
		The first integer element will be the value of parent. 
		
		
		The second integer will be the value of corresponding left or right child. In case the child is null, this value will be -1.
		
		
		The third element of triplet which is a character can take any of the three values ‘L’, ‘R’ or ‘N’. L denotes that the children is a left child, R denotes that the children is a Right Child and N denotes that the child is NULL.
		 
		
	
	
Please note that the relationships are printed only for internal nodes and not for leaf nodes.
Output:
The function should return the sum of all the nodes of the binary tree.
User Task:
As it is a functional problem. So, you don't need to worry about the input you just have to complete the function sumBT() that takes a node as a parameter and returns the sum of all the nodes.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
2
1 2 L 1 -1 N
6
1 2 L 1 3 R 2 -1 N 2 -1 N 3 3 L 3 -1 N
Output:
3
9
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Starter code: #User function Template for python3



def sumBT(root):

    #code

Solutions:
def sumBT(root):
	if root is None:
		return 0
	return root.data + sumBT(root.left) + sumBT(root.right)


def sumBT(root):
	if not root:
		return 0
	return root.data + sumBT(root.left) + sumBT(root.right)


def sumBT(root):
	res = 0

	def sumAll(r):
		if not r:
			return 0
		nonlocal res
		res += r.data
		return sumAll(r.left) + sumAll(r.right)
	sumAll(root)
	return res


def sumBT(root):
	if root == None:
		return 0
	l = sumBT(root.left)
	r = sumBT(root.right)
	return l + r + root.data


def sumBT(root):
	sum = 0
	if root is not None:
		sum += sumBT(root.left)
		sum += root.data
		sum += sumBT(root.right)
	return sum


def sumBT(root):
	sum = 0
	if root is None:
		return 0
	sum = sum + root.data
	lsum = sumBT(root.left)
	rsum = sumBT(root.right)
	return sum + lsum + rsum


def sumBT(root):
	if root == None:
		return 0
	else:
		return root.data + sumBT(root.left) + sumBT(root.right)


def sumBT(root):
	stack = [(root, False)]
	ans = 0
	while stack:
		(node, visited) = stack.pop()
		if visited:
			ans += node.data
		elif node:
			stack.append((node.right, False))
			stack.append((node, True))
			stack.append((node.left, False))
	return ans


def sumBT(root):
	stack = []
	sum = 0
	while True:
		if root:
			sum += root.data
			stack.append(root)
			root = root.left
		elif stack:
			root = stack.pop()
			root = root.right
		else:
			break
	return sum


def sumBT(root):
	stack = [root]
	answer = 0
	while stack:
		node = stack.pop()
		answer += node.data
		if node.right:
			stack.append(node.right)
		if node.left:
			stack.append(node.left)
	return answer


def sumBT(root):
	if root == None:
		return 0
	if root.left == None and root.right == None:
		return root.data
	return sumBT(root.left) + sumBT(root.right) + root.data


def sumBT(root):

	def get_val(w, r):
		r += w.data
		if w.right is not None:
			r = get_val(w.right, r)
		if w.left is not None:
			r = get_val(w.left, r)
		return r
	result = get_val(root, 0)
	return result


def sumBT(root):
	nodes_l = 0
	nodes_r = 0
	if root.left:
		nodes_l = sumBT(root.left)
	if root.right:
		nodes_r = sumBT(root.right)
	if not root.left and (not root.right):
		return root.data
	return nodes_l + nodes_r + root.data


def sumBT(root):

	def calcSum(curr):
		if not curr:
			return 0
		return calcSum(curr.left) + calcSum(curr.right) + curr.data
	return calcSum(root)


def sumBT(root):
	if root == None:
		return 0
	else:
		ls = sumBT(root.left)
		rs = sumBT(root.right)
		return root.data + ls + rs


def sumBT(root):
	l = [root]
	sum = 0
	while l:
		t = l.pop(0)
		sum += t.data
		if t.left:
			l.append(t.left)
		if t.right:
			l.append(t.right)
	return sum


def fun(root, l):
	if root:
		l.append(root.data)
		fun(root.left, l)
		fun(root.right, l)

def sumBT(root):
	l = []
	fun(root, l)
	return sum(l)


def sumBT(root):
	if root is None:
		return 0
	leftsum = sumBT(root.left)
	rightsum = sumBT(root.right)
	return leftsum + rightsum + root.data


def sumBT(root):
	r = 0
	if root:
		r += sumBT(root.left)
		r += root.data
		r += sumBT(root.right)
	return r


from collections import deque

def sumBT(root):
	res = []
	q = deque()
	q.append(root)
	while len(q):
		node = q.popleft()
		res.append(node.data)
		if node.left:
			q.append(node.left)
		if node.right:
			q.append(node.right)
	return sum(res)


def sumBT(root):
	if root is None or root.data == 'L' or root.data == 'R' or (root.data == 'N') or (root.data == -1):
		return 0
	return sumBT(root.left) + sumBT(root.right) + root.data


def sumBT(root):
	sum = 0
	if not root:
		return 0
	if not root.left and (not root.right):
		return root.data
	return root.data + sumBT(root.left) + sumBT(root.right)


def sumBT(root):
	if root is None:
		return 0
	stack = [root]
	ans = 0
	while stack:
		node = stack.pop()
		ans += node.data
		if node.right:
			stack.append(node.right)
		if node.left:
			stack.append(node.left)
	return ans


def sumBT(root):
	sum_tree = []
	sum_helper(root, sum_tree)
	return sum(sum_tree)

def sum_helper(root, sum_tree):
	if root:
		sum_tree.append(root.data)
		sum_helper(root.left, sum_tree)
		sum_helper(root.right, sum_tree)


def sumBT(root):
	l = []

	def ino(root):
		if root is not None:
			ino(root.left)
			l.append(root.data)
			ino(root.right)
	ino(root)
	return sum(l)


def sumBT(root):
	if root is None:
		return 0
	leftSum = sumBT(root.left)
	rightSum = sumBT(root.right)
	return leftSum + rightSum + root.data


def sumBT(root):
	re = []

	def trav(r):
		if r.left is not None:
			trav(r.left)
		if r.right is not None:
			trav(r.right)
		re.append(r.data)
	trav(root)
	return sum(re)


def sumBT(root):
	if root is None:
		return 0
	else:
		sum1 = sumBT(root.left)
		sum2 = sumBT(root.right)
		sum = sum1 + sum2 + root.data
	return sum