AdSense

Friday, December 12, 2014

Find the sum of a binary tree

A Google interview question, very easy tree traversal problem.


public class TreeSum {
 public int findSum(TreeNode root)
 {
  if (root == null)
   return 0;
  int sum = 0;
  sum = findSum(root.left) + findSum(root.right) + root.val;
  return sum;
 }
}

No comments:

Post a Comment