1.size()--------------------
Given a binary tree, count the number of nodes in the tree.
int size(struct node* node) { }
2.maxDepth()/ height()---------------both iterative and recursive ways
Given a binary tree, compute its "maxDepth" -- the number of nodes along the longest path from the root node down to the farthest leaf node
int maxDepth(struct node* node) {
http://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/
http://www.geeksforgeeks.org/iterative-method-to-find-height-of-binary-tree/
Modified:
http://www.geeksforgeeks.org/find-height-of-a-special-binary-tree-whose-leaf-nodes-are-connected/
3.CountLeaf()----------
Write a C Program to count leaf nodes in a binary tree.
A node is a leaf node if both left and right child nodes of it are NULL.
4.IsBTHeightBalanced()
5.Get level of a node in a binary tree
Solutions:
1.Size of a Binary Tree
i)Recursive
int maxDepth(struct node* node) {
if (node==NULL) {
return(0);
}
else {
// compute the depth of each subtree
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
// use the larger one
if (lDepth > rDepth) return(lDepth+1);
else return(rDepth+1);
}
}
http://algorithms.tutorialhorizon.com/find-the-maximum-depth-or-height-of-a-binary-tree/
ii)Iterative
http://www.leetcode.com/2010/04/maximum-height-of-binary-tree.html
3.Min Value & Max Value
int size(struct node* node) { }
2.maxDepth()/ height()---------------both iterative and recursive ways
Given a binary tree, compute its "maxDepth" -- the number of nodes along the longest path from the root node down to the farthest leaf node
int maxDepth(struct node* node) {
http://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/
http://www.geeksforgeeks.org/iterative-method-to-find-height-of-binary-tree/
Modified:
http://www.geeksforgeeks.org/find-height-of-a-special-binary-tree-whose-leaf-nodes-are-connected/
3.CountLeaf()----------
Write a C Program to count leaf nodes in a binary tree.
A node is a leaf node if both left and right child nodes of it are NULL.
4.IsBTHeightBalanced()
5.Get level of a node in a binary tree
Solutions:
1.Size of a Binary Tree
int
size(
struct
tree* root)
{
if
(root==NULL)
return
0;
else
return
(size(root->left) + 1 + size(root->right));
}
2.Max Depth or Heighti)Recursive
int maxDepth(struct node* node) {
if (node==NULL) {
return(0);
}
else {
// compute the depth of each subtree
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
// use the larger one
if (lDepth > rDepth) return(lDepth+1);
else return(rDepth+1);
}
}
http://algorithms.tutorialhorizon.com/find-the-maximum-depth-or-height-of-a-binary-tree/
ii)Iterative
http://www.leetcode.com/2010/04/maximum-height-of-binary-tree.html
3.Min Value & Max Value
Concept: Given a non-empty binary search tree, return the minimum data value found in that tree. Note that the entire tree does not need to be searched. int minValue( struct node* node) { |
struct node* current = node; |
while (current->left != NULL { |
current = current->left;
} return (current->data); }
4.Counting Leaf Nodes unsigned int getLeafCount(struct node* node) { if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left)+getLeafCount(node->right); }
An empty tree is height-balanced. A non-empty binary tree T is balanced if: 1) Left subtree of T is balanced 2) Right subtree of T is balanced 3) The difference between heights of left subtree and right subtree is not more than 1. bool isBalanced( struct node *root) { int lh;
int 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 1; return 0; } 6.Get Level of a node in a binary tree: int getLevel(struct node* root, int n, int level)
{
if(root == NULL)
return 0;
if(root->data == n)
return level;
return getLevel(root->left, n, level+1) | getLevel(root->right, n, level+1);
}
7.Get the Height of a Node in a Binary Tree http://algorithms.tutorialhorizon.com/get-the-height-of-a-node-in-a-binary-tree/
8.Diameter of a Binary Tree[BT] http://www.geeksforgeeks.org/diameter-of-a-binary-tree/
9.Maximum Width of a Binary Tree http://www.geeksforgeeks.org/maximum-width-of-a-binary-tree/
10.Number of possible BSTs with n nodes
http://www.geeksforgeeks.org/g-fact-18/
11.Delete a tree |
int hasPathSum(struct node* node, int sum) {
ReplyDeleteint sum,subSum;
// return true if we run out of tree and sum==0
if (node == NULL) {
return(sum == 0);
}
else {
// otherwise check both subtrees
subSum = sum - node->data;
return(hasPathSum(node->left, subSum) ||
hasPathSum(node->right, subSum));
}
}
http://shashank7s.blogspot.com/2011/02/wap-to-findout-vrtical-sum-of-nodes.html
ReplyDeletehttp://techpuzzl.wordpress.com/2010/06/29/vertical-sum-of-a-binary-tree/
ReplyDelete