# Day18: (Binary Tree)

[**Level order Traversal**](https://practice.geeksforgeeks.org/problems/level-order-traversal/1)\
Solved on: Aug 1st 2020

```cpp
int height (Node *node) {
    int lh, rh;
    if (node==NULL) {
        return 0;
    }
    
    lh=height(node->left);
    rh= height(node->right);
    
    if (lh>rh){
        return lh+1;
    } else {
        return rh+1;
    }
}

  
/* Print nodes at a given level */
void printGivenLevel(Node* root, int level, vector<int> &v) {  
    if (root == NULL)  
        return;  
    if (level == 1)  
        v.push_back(root->data);
    else if (level > 1)  
    {  
        printGivenLevel(root->left, level-1, v);  
        printGivenLevel(root->right, level-1, v);  
    }  
}  

vector<int> levelOrder(Node* node) {
    vector<int> v;
    if (node == NULL) {
        return v;
    }
    
    int h=height(node);
    int i;
    for (i=0;i<=h;i++) {
        printGivenLevel(node, i, v);
    }
    return v;
}
```

[**Height of a Binary Tree**](https://leetcode.com/problems/maximum-depth-of-binary-tree/)\
Solved on: Aug 1st 2020

```cpp
int maxDepth(TreeNode* node) {
    int lh, rh;
    if (node==NULL) {
        return 0;
    }

    lh=maxDepth(node->left);
    rh= maxDepth(node->right);

    if (lh>rh){
        return lh+1;
    } else {
        return rh+1;
    }
}
```

[**Diameter of Binary Tree**](https://leetcode.com/problems/diameter-of-binary-tree/)\
Solved on: Aug 1st 2020

```cpp
int diameterOfBinaryTree(TreeNode* root) {
    if (root == NULL) {
        return 0;
    }
    
    int lh=0, rh=0, ld=0, rd=0;
    
    lh = height(root->left);
    rh = height(root->right);
    
    int th = lh +  rh ;
    
    ld=diameterOfBinaryTree(root->left);
    rd=diameterOfBinaryTree(root->right);
    
    int diameter = max(ld, rd);
    return max(diameter, th);
    
}

int height(TreeNode* node) {
    if (node == NULL) {
        return 0;
    }
    else {
        return max(height(node->left)+1, height(node->right)+1);
    }
}
```

[**Check if Binary tree is height balanced or not** ](https://practice.geeksforgeeks.org/problems/check-for-balanced-tree/1)\
Solved on: Aug 2nd 2020

```cpp
int height(Node *root) {
    if (root==NULL) {
        return 0;
    }
    
    return 1 + max(height(root->left), height(root->right)); 
}

bool isBalanced(Node *root) {
    int lh, rh;
    if (root==NULL) {
        return 1;
    }
    
    lh=height(root->left);
    rh=height(root->right);
    
    if (abs(lh-rh) <=1 && isBalanced(root->left) && isBalanced(root->right)) {
        return true;
    }
    return false;
}

```

[**Lowest Common Ancestor of a Binary Tree**](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)\
\&#xNAN;***Solved on: Aug 2nd 2020***

```cpp
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    if ( !root || root->val == p->val || root->val == q->val ) {
        return root;
    }
    
    TreeNode* left_lca  = lowestCommonAncestor(root->left, p, q);
    TreeNode* right_lca  = lowestCommonAncestor(root->right, p, q);
    
    
    if (left_lca && right_lca) {
        return root;
    }
    
    return left_lca != NULL ? left_lca : right_lca;
    
}
```

[**Check if two trees are identical or not** ](https://leetcode.com/problems/same-tree/submissions/)\
Solved on: Aug 2nd 2020

```cpp
bool isSameTree(TreeNode* p, TreeNode* q) {
    if (p==NULL && q==NULL ) {
        return true;
    }
    if (p != NULL && q != NULL) {
        return p ->val == q->val &&  isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
    return false;
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gopavasanth.gitbook.io/code/sde-problems/day18-binary-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
