Professional Documents
Culture Documents
if (level == 1)
{
cout << root->key << " ";
if (level == 1)
{
cout << root->key << " ";
return true;
}
if (level == 1)
{
cout << root->key << " ";
return true;
}
// Function to find the distance between node 'x' and node 'y' in a
// given binary tree rooted at 'root' node
int findDistance(Node *root, Node* x, Node* y)
{
// lca stores lowest common ancestor of x and y
Node *lca = nullptr;
// call LCA procedure only if both x and y are present in the tree
if (isNodePresent(root, y) && isNodePresent(root, x))
lca = findLCA(root, x, y);
else
return INT_MIN;
return maxWidth;
}
if(root == NULL)
return 0;
if(level == 1)
return 1;
int level = 0;
// If this node is a leaf and its level is odd, return its level
if (root->left==NULL && root->right==NULL && level&1)
return level;
// If not leaf, return the maximum value from left and right subtrees
return max(depthOfOddLeafUtil(root->left, level+1),
depthOfOddLeafUtil(root->right, level+1));
}
/* Main function which calculates the depth of deepest odd level leaf.
This function mainly uses depthOfOddLeafUtil() */
int depthOfOddLeaf(struct Node *root)
{
int level = 1, depth = 0;
return depthOfOddLeafUtil(root, level);
}
// Update result if this node is left leaf and its level is more
// than the maxl level of the current result
if (isLeft && !root->left && !root->right && lvl > *maxlvl)
{
*resPtr = root;
*maxlvl = lvl;
return;
}
int isSibling(struct Node *root, struct Node *a, struct Node *b)
{
// Base case
if (root==NULL) return 0;