Created
August 18, 2020 05:31
-
-
Save misterpoloy/c492d2a650e9c4d17d08b3b1eb4f746a to your computer and use it in GitHub Desktop.
Node Depths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
// Repasado 2 veces | |
using namespace std; | |
class BinaryTree { | |
public: | |
int value; | |
BinaryTree *left; | |
BinaryTree *right; | |
BinaryTree(int value) { | |
this->value = value; | |
left = NULL; | |
right = NULL; | |
} | |
}; | |
int countNodeDepth(BinaryTree* root, int depth = 0) { | |
if (root == NULL) return 0; | |
int left = countNodeDepth(root->left, depth + 1); | |
int right = countNodeDepth(root->right, depth + 1); | |
return left + right + depth; | |
} | |
int nodeDepths(BinaryTree *root) { | |
return countNodeDepth(root); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.