Skip to content

Commit

Permalink
finish maximum depth of N-ary tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Checky-Hu committed Mar 4, 2021
1 parent 4559eb8 commit 71f6f14
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 12-50/maximum-depth-of-N-ary-tree/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
if [ ! -d target ];then
mkdir target
fi

g++ -Wall -g main.cc -o target/main
49 changes: 49 additions & 0 deletions 12-50/maximum-depth-of-N-ary-tree/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>

using namespace std;

class Node {
public:
int val;
vector<Node*> children;

Node() {}

Node(int _val) {
val = _val;
}

Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};

class Solution {
public:
int maxDepth(Node* root) {
if (root == nullptr) {
return 0;
}
int max = 0;
for (vector<Node*>::const_iterator iter = root->children.begin();
iter != root->children.end(); iter++) {
int tmp = maxDepth(*iter);
if (tmp > max) {
max = tmp;
}
}
return max + 1;
}
};

int main(int argc, char ** argv)
{
vector<Node*> children;
children.push_back(new Node(2));
children.push_back(new Node(3));
Node* root = new Node(1, children);
cout << Solution().maxDepth(root) << endl;
return 0;
}

0 comments on commit 71f6f14

Please sign in to comment.