Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
U

User 11532880

@User 11532880
About
Posts
5
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Binary Tree Recursion Problem
    U User 11532880

    You solved it, thanks!

    C / C++ / MFC asp-net linux data-structures help

  • Binary Tree Recursion Problem
    U User 11532880

    That works, but I wanted to understand why the other version does not work? Here is all the code if you want to try.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct treenode {
    int num;
    struct treenode * left;
    struct treenode * right;
    } TreeNode;

    int recLookup (TreeNode *, int);
    int lookUp(TreeNode *, int);
    int sizeRec(TreeNode *);
    int maxDepth(TreeNode *);
    int minValue(TreeNode *);
    int maxValue(TreeNode *);
    int hasPathSum(TreeNode *, int);

    TreeNode * createNode (int);
    TreeNode * insertRec(TreeNode *, int);
    TreeNode * buildTree();

    void deleteRec(TreeNode *);
    void printIncreasing(TreeNode *);
    void printPostOrder(TreeNode *);
    void printPaths(TreeNode *);
    void printPathRecur(TreeNode *, int[], int);

    enum { FALSE, TRUE };

    int main (void) {
    TreeNode * root = buildTree();
    printf("Size: %d\n", sizeRec(root));
    printf("Max Depth: %d\n", maxDepth(root));
    printf("Min Value: %d\n", minValue(root));
    printf("Max Value: %d\n", maxValue(root));
    printIncreasing(root); puts("");
    printPostOrder(root); puts("");
    printf("Has path sum 15: %d\n", hasPathSum(root, 15));
    printPaths(root);
    // not sure why line below throws segmentation core
    printf("Min Val: %d Max Val: %d\n", minValue(root), maxValue(root));

    deleteRec(root);
    
    
    return 0;
    

    }

    void printPaths(TreeNode * root) {
    int path[10];
    int pathLen = 0;
    printPathRecur(root, path, pathLen);
    }

    void printPathRecur(TreeNode * node, int path[], int pathLen) {
    if (node == NULL) return;
    path[pathLen] = node->num;
    pathLen++;
    if (node->right == NULL && node->left == NULL) {
    int i;
    for (i = 0; i < pathLen; i++)
    printf("%d ", path[i]);
    puts("");
    }
    else {
    printPathRecur(node->left, path, pathLen);
    printPathRecur(node->right, path, pathLen);
    }
    }

    int hasPathSum(TreeNode * root, int num) {
    TreeNode * current = root;
    if (current == NULL)
    return num == 0;
    else {
    int n = num - current->num;
    return
    hasPathSum(current->left, n) ||
    hasPathSum(current->right, n);
    }
    }

    void printPostOrder(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL) return;
    printPostOrder(current->left)

    C / C++ / MFC asp-net linux data-structures help

  • Binary Tree Recursion Problem
    U User 11532880

    I'm sorry, the recursion is in other functions, not in this one. Do you know why it's not working when they are called together?

    C / C++ / MFC asp-net linux data-structures help

  • Binary Tree Recursion Problem
    U User 11532880

    I will paste the code. But they are working fine when they are called individually.

    int minValue(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL) return -1;
    else {
    while (current->left != NULL)
    current = current->left;
    return current->num;
    }
    }

    int maxValue(TreeNode * root) {
    TreeNode * current = root;
    if (current == NULL)
    return -1;
    else
    {
    while (current->right != NULL)
    current = current->right;
    return current->num;
    }
    }

    And they are called as posted above. One more time, when called individually they work fine.

    C / C++ / MFC asp-net linux data-structures help

  • Binary Tree Recursion Problem
    U User 11532880

    Does somebody have an idea why will the following happen: printf("Min Value: %d\n", minValue(root)); printf("Max Value: %d\n", maxValue(root)); // not sure why line below throws segmentation core //printf("Min Val: %d Max Val: %d\n", minValue(root), maxValue(root)); minValue and maxValue are recursive functions returning the min/max value of a binary tree, when called separately as in lines 1 & 2 they work fine, but when called from the same printf statement I get a segmenation fault in linux. Thanks!

    C / C++ / MFC asp-net linux data-structures help
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups