Advertisement
satishfrontenddev5

Untitled

Feb 22nd, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Given a binary tree, find its maximum depth.
  2.  
  3. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
  4.  
  5. Note: A leaf is a node with no children.
  6.  
  7. Input format
  8. Line1: Number of Test cases (T)
  9.  
  10. Line2 to X: First Test Case’s binary tree structure (refer section below for the format)
  11.  
  12. LineX+1 to Y: Second Test Case’s binary tree structure and so on.
  13.  
  14. Output format
  15. Print the maximum height of the tree.
  16.  
  17. Sample Input 1
  18. 1
  19.  
  20. 6
  21.  
  22. 1 2 3 4 5 6
  23.  
  24. 1 2 3
  25.  
  26. 2 4 -1
  27.  
  28. 3 -1 5
  29.  
  30. 4 -1 6
  31.  
  32. 5 -1 -1
  33.  
  34. 6 -1 -1
  35.  
  36. Sample Output 1
  37. 4
  38.  
  39. Explanation 1
  40. Input Visualization:
  41.  
  42. image
  43.  
  44. In the given input the height of the tree is 4.
  45.  
  46. Constraints
  47. 1<=T<=100
  48.  
  49. 1<=N<=100000
  50.  
  51. Sum of the number of nodes in all test cases will be less than 500000.
  52.  
  53. Instructions to create custom input for a Binary Tree
  54. In order to specify a binary tree that can be used as custom input to the problem, you’ll need to follow this convention.
  55.  
  56. Line 1: Number of nodes in the Binary Tree (N)
  57.  
  58. Line 2: N space separated node values. The position of the Nodes on this line will be used to refer to them in the below lines, starting from 1.
  59.  
  60. Line 3 to N+2: Lines specifying the child nodes for each of the N nodes
  61.  
  62.  
  63. Format of each line (space separated): Parent_node Left_child_node Right_child_node
  64.  
  65. Parent_node - Node at this Position on Line 2 is the Node to which we are assigning the Left and Right child here
  66.  
  67. Left_child_node - Node at this position on Line 2 is the left child. Specify -1 if there is no Left child.
  68.  
  69. Right_child_node - Node at this position on Line 2 is the right child. Specify -1 if there is no Right child.
  70.  
  71. Example1
  72. image
  73. /*
  74. // Definition for TreeNode
  75. class TreeNode {
  76. public:
  77.     long val;
  78.     TreeNode* left;
  79.     TreeNode* right;
  80.     TreeNode* next;
  81.     TreeNode (long x) {
  82.         val = x;
  83.         left = NULL;
  84.         right = NULL;
  85.         next = NULL;
  86.     }
  87. */
  88. #include <bits/stdc++.h>
  89. #include "../crio/cpp/io/FastIO.hpp"
  90. #include "../crio/cpp/ds/TreeNode/TreeNode.hpp"
  91.  
  92. using namespace std;
  93.  
  94. class MaximumDepthOfBinaryTree {
  95. public:
  96.     int maxDepth(TreeNode* root) {
  97.     }
  98. };
  99.  
  100. int main() {
  101.     FastIO();
  102.     int t;
  103.     cin >> t;
  104.     while (t--) {
  105.         TreeNode* root  = TreeNode().readTreeReturnRoot();
  106.         int result = MaximumDepthOfBinaryTree().maxDepth(root);
  107.         cout << result << endl;
  108.     }
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement