Editing
2019 Dec Platinum Problem 3 Tree Depth
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Official Problem Statement == [http://www.usaco.org/index.php?page=viewproblem2&cpid=974 Tree Depth] Problem Statement: Given a rooted tree with N nodes, find the maximum depth of the tree. Solution: The maximum depth of a tree can be found by traversing the tree and keeping track of the maximum depth seen so far. In C++, this can be done using a recursive function: <pre> int maxDepth(Node* root) { if (root == NULL) return 0; int left_depth = maxDepth(root->left); int right_depth = maxDepth(root->right); return max(left_depth, right_depth) + 1; } </pre> The function takes the root node of the tree as an argument and returns the maximum depth of the tree. It first checks if the root is NULL, in which case the depth is 0. Otherwise, it recursively calls the function on the left and right subtrees, and returns the maximum of the two depths plus 1 (to account for the current node). [[Category:Yearly_2019_2020]] [[Category:Platinum]] [[Category:Dynamic Programming]] [[Category:Tree]] [[Category:DFS]] [[Category:Centroid Decomposition]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information