Find Binary Tree Path Sum I am trying to resolve following problem. Please advice which part of the code can be improved.
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Example:
Given the below binary tree and sum = 22,
      5
     / \
    4   8
   /   / \
  11  13  4
 /  \      \
7    2      1

return true, as there exist a root-to-leaf path 5-&gt;4-&gt;11-&gt;2 which sum is 22.
Implementation:
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdbool.h&gt;

typedef struct stTree
{
    struct stTree * left;
    struct stTree * right;
    int value;
} stTree;

struct stTestCase
{
   stTree *root;
   int sum;
   bool result;
};

stTree* createNode(int value, stTree *leftNode, stTree *rightNode)
{
    stTree *node = malloc(sizeof *node);
    if (!node) abort();
    node-&gt;left = leftNode;
    node-&gt;right = rightNode;
    node-&gt;value = value;
    return node;
}

bool hasPathSum(const stTree *root, int sum)
{
    if (!root) return false;

    if (!root-&gt;left &amp;&amp; !root-&gt;right)
    {
        if (sum == root-&gt;value)
            return true;
        else
            return false;
    }
    return (hasPathSum(root-&gt;left, sum - root-&gt;value)
      | hasPathSum(root-&gt;right, sum - root-&gt;value));
}

static stTree* findBottomLeft(stTree *node)
{
    while (node-&gt;left)
        node = node-&gt;left;
    return node;
}

void freeTree(stTree *node)
{
    if (!node) return true;
    stTree *bottomLeft = findBottomLeft(node);

    while (node)
    {
        if (node-&gt;right)
        {
            bottomLeft-&gt;left = node-&gt;right;
            bottomLeft = findBottomLeft(bottomLeft);
        }
        stTree *old = node;
        node = node-&gt;left;
        free(old);
    }
}

int main(void)
{

   struct stTestCase test_cases[3]={
        {
           createNode(5,
            createNode(4,
                createNode(11,
                    createNode(7, NULL, NULL),
                    createNode(2, NULL, NULL)), NULL),
            createNode(8,
                createNode(13, NULL, NULL),
                createNode(4, NULL,
                    createNode(1, NULL, NULL)))),
            22,
            true
         },
         {
            createNode(5,
             createNode(1,
                 createNode(11,
                     createNode(3, NULL, NULL),
                     createNode(2, NULL, NULL)), NULL),
             createNode(7,
                 createNode(13, NULL, NULL),
                 createNode(4, NULL,
                     createNode(1, NULL, NULL)))),
            22,
            false
          },
          {
             createNode(12,
              createNode(4,
                  createNode(11,
                      createNode(7, NULL, NULL),
                      createNode(51, NULL, NULL)), NULL),
              createNode(8,
                  createNode(13, NULL, NULL),
                  createNode(4, NULL,
                      createNode(6, NULL, NULL)))),
               22,
               false
           }
   };

   for(int i=0; i&lt;3; i++)
   {
      if (hasPathSum(test_cases[i].root, test_cases[i].sum)  == test_cases[i].result)
         printf(&quot;test case [%d], PASS\n&quot;, i);
      else
        printf(&quot;test case [%d], FAIL\n&quot;, i);
      freeTree(test_cases[i].root);
   }

}

RESULT:
test case [0], PASS
test case [1], PASS
test case [2], PASS
We don't normally do this in real (non-toy) programs:

stTree *node = malloc(sizeof *node);
if (!node) abort();


It's convenient and probably okay for a simple exercise like this, though.

Code of the form if (c) then return true; else return false; simplifies to just return c;, so we can reduce
    if (sum == root-&gt;value)
        return true;
    else
        return false;

to just
    return sum == root-&gt;value;

I think we can do better than that, if we're happy to consider an empty tree as having a sum of zero:
bool hasPathSum(const stTree *node, int sum)
{
    if (!node) {
        return sum == 0;
    }

    sum -= node-&gt;value;
    return hasPathSum(node-&gt;left, sum) || hasPathSum(node-&gt;right, sum);
}

Notice that I've used || rather than | in my version - there's no need to continue looking once we've found a successful result.  Also, because this function is recursive, the name root is misleading for the parameter - it could be any node, and it's only the root in the top-level call.


void freeTree(stTree *node)
{
    if (!node) return true;


That's a simple error - can't return a value from a function declared void(...).  That suggests you're not enabling enough compiler warnings.

Good to see unit tests.  I'm not sure I'd pile all the test data together like that - I'd probably create a small function for each test, so that each test is independent.  Consider using one of the available test frameworks to help relieve the repetitive work of testing.
When writing tests, it's a good idea to write the smallest test you can think of (e.g. the null tree) as the first test, and start with it failing (so you know the test works).  Then make it pass, and write the next simplest failing tests (tree with one node - make sure you test calls that should return different values).
Here's how I would begin (using Google Test, which is C++, but it's easy to test C code by using extern &quot;C&quot;):
#include &lt;gtest/gtest.h&gt;

extern &quot;C&quot; {
    struct stTree;

    stTree* createNode(int value, stTree *leftNode, stTree *rightNode);
    bool hasPathSum(const stTree *node, int sum);
    void freeTree(stTree *node);
}

TEST(PathSum, empty)
{
    stTree *tree = NULL;
    EXPECT_FALSE(hasPathSum(tree, 1));
    EXPECT_TRUE(hasPathSum(tree, 0));
}

TEST(PathSum, one_node)
{
    stTree *tree = createNode(2, NULL, NULL);
    EXPECT_FALSE(hasPathSum(tree, 0));
    EXPECT_TRUE(hasPathSum(tree, 2));
}

TEST(PathSum, two_nodes)
{
    stTree *tree = createNode(2, NULL,
                              createNode(3, NULL, NULL));
    EXPECT_FALSE(hasPathSum(tree, 0));
    EXPECT_TRUE(hasPathSum(tree, 2));
    EXPECT_TRUE(hasPathSum(tree, 5));
}