SHARE
TWEET

Untitled

a guest Sep 18th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct treeNode {
  6.     treeNode* left;
  7.     treeNode* right;
  8.     char val;
  9.     treeNode(int x) : val(x), left(NULL), right(NULL) {}
  10. };
  11. int maxDepth = 0;
  12.  
  13. void traverse(treeNode* node, int depth){
  14.     if(node == NULL)
  15.         return;
  16.     depth++;
  17.     if(depth > maxDepth)
  18.         maxDepth = depth;
  19.  
  20.  
  21.     traverse(node->left, depth);
  22.     traverse(node->right, depth);
  23. }
  24.  
  25. int main(){
  26.  
  27.     treeNode* head = new treeNode('F');
  28.     treeNode* temp = head;
  29.  
  30.     temp->left = new treeNode('B');
  31.     temp = temp->left;
  32.     temp->left = new treeNode('A');
  33.     temp->right = new treeNode('D');
  34.     temp = temp->right;
  35.  
  36.     temp->left = new treeNode('C');
  37.     temp->right = new treeNode('E');
  38.  
  39.     temp = head;
  40.     temp->right = new treeNode('G');
  41.     temp = temp->right;
  42.     temp->right = new treeNode('I');
  43.     temp->right->left = new treeNode('H');
  44.     temp->right->left->right = new treeNode('J');
  45.  
  46.     traverse(head, 0);
  47.  
  48.     cout << maxDepth;
  49.  
  50.     return 0;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top