dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: tree level

  1. #1
    Join Date
    May 2018
    Posts
    84

    tree level

    Can you confirm this code is right to calculate level of binary tree (tree depth) ?

    Code:
    // albero vuoto = -1
    // livello albero min. = 0
    int tree_depth(Node* tree) {
       if (!tree) return -1;
       return ( 1 + max( tree_depth(tree->left), tree_depth(tree->right) ) );
    }
    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: tree level

    Quote Originally Posted by zio_mangrovia View Post
    Can you confirm this code is right to calculate level of binary tree (tree depth) ?

    Code:
    // albero vuoto = -1
    // livello albero min. = 0
    int tree_depth(Node* tree) {
       if (!tree) return -1;
       return ( 1 + max( tree_depth(tree->left), tree_depth(tree->right) ) );
    }
    Thanks
    Didn't you test it?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2018
    Posts
    84

    Re: tree level

    Quote Originally Posted by VictorN View Post
    Didn't you test it?
    I saw multiple versions and I would like to be sure It's right one.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: tree level

    Quote Originally Posted by zio_mangrovia View Post
    I saw multiple versions and I would like to be sure It's right one.
    Then test it for multiple use cases!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)