public class BinaryTree {
Node root;
static class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
}
// Function to find the sum of all nodes of a binary tree
public int sumNodes(Node node) {
if (node == null)
return 0;
return (node.data + sumNodes(node.left) + sumNodes(node.right));
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(10);
tree.root.left = new Node(20);
tree.root.right = new Node(30);
tree.root.left.left = new Node(40);
tree.root.left.right = new Node(50);
tree.root.right.left = new Node(60);
tree.root.right.right = new Node(70);
int sum = tree.sumNodes(tree.root);
System.out.println("Sum of all nodes of the binary tree is " + sum);
}
}