Given a BST with 2 nodes swapped, fix it

Problem Given a BST with 2 nodes swapped fix it. Example Consider the BST: Following is the correct BST 10 / \\ 5 20 / \\ 2 8 Now we swap 8 and 20, and BST is changed. Input Tree: 10 / \\ 5 8 / \\ 2 20 In the above tree, nodes 20 and 8 must be swapped to fix the tree. ```In the [previous post](http://k2code.blogspot.in/2015/09/given-binary-search-tree-with-2-nodes.html), we saw how many pairs in the input tree violate the BST property. [Read More]

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties

Problem Given a binary search tree with 2 nodes swapped, give the number of nodes not following bst property. Follow up - Fix the BST, in the next post. Example Consider the BST: Following is the correct BST 10 / \\ 5 20 / \\ 2 8 Now we swap 8 and 20, and BST is changed. Input Tree: 10 / \\ 5 8 / \\ 2 20 In the above tree, nodes 20 and 8 must be swapped to fix the tree. [Read More]

Given a binary tree with extra field "next pointer" in node, populate the next pointer to the nodes inorder successor

Problem : Given a Binary tree where each node has an extra field (node * next), write a function that puts the inorder successor of that node in the next pointer. This is how our binary tree node looks like : struct node { int data; struct node\* left; struct node\* right; struct node\* next; } Initially, all next pointers have NULL values. Your function should fill these next pointers so that they point to inorder successor. [Read More]

Binary Tree In-Order Traversal - Recursive and Iterative Solution

Consider the tree Inorder traversal prints the binary tree in increasing order in case of Binary Search Tree, but here we are discussing any binary tree. To traverse a binary tree in Inorder, following operations are carried-out : Traverse the left most subtree starting at the left external node,  Visit the root, and Traverse the right subtree starting at the left external node. Therefore, the Inorder traversal of the above tree will outputs: [Read More]

Binary tree traversal: Preorder, Inorder, Post-order, BFS, DFS, Level Order, zig zag order

In order to illustrate the 3 traversals - pre order, in-order and post-order lets take following tree: Preorder traversal: To traverse a binary tree in Preorder, following operations are carried-out (i) Visit the root, (ii) Traverse the left subtree, and (iii) Traverse the right subtree. Therefore, the Preorder traversal of the above tree will outputs: 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10 Implementing pre-order traversal [Read More]

Given a binary tree, print the sum( inclusive of the root ) of sub-tree rooted at each node in in-order

Given a binary tree. Find the sum( inclusive of the root ) of sub-tree rooted at each node. Print the sum in in-order.

E.g.

                  3  
  
                /  \\  
  
             2       4  
  
           /        /  \\  
  
         2         2   -1  

Output: 2, 4, 12, 2, 5, -1.

Solution

#include <stdio.h>  
#include <stdlib.h>  
  
typedef struct TreeNode  
{  
    struct TreeNode \*left, \*right;  
    int data;  
  
}TreeNode;  
  
typedef TreeNode \* Tree;  
  
  
/\*  
Two passes with constant space  
\*/  
int printSum(Tree t, int toPrint)  
{  
    if(t == NULL)  
        return 0;  
  
    int lsum = printSum(t->left, toPrint);  
    int rsum = printSum(t->right, 0);  
  
    int sum = lsum + t->data + rsum;  
  
    if(toPrint)  
        printf("%d ", sum);  
  
    printSum(t->right, toPrint);  
  
    return sum;  
  
}  
  
  
/\*  
One pass with n space  
\*/  
int printSum2(Tree t, int a\[\], int \*pos)  
{  
    if(t == NULL)  
        return 0;  
  
    int lsum = printSum2(t->left, a, pos);  
  
    (\*pos)++;  
  
    int p = \*pos;  
  
    int rsum = printSum2(t->right,a, pos);  
  
    return a\[p\] = lsum + t->data + rsum;  
  
  
}  
  
/\*Utilities\*/  
  
inline  TreeNode \* makeTreeNode(int data)  
{  
    TreeNode \*n = calloc(sizeof(TreeNode), 1);  
    n->data = data;  
  
    return n;  
}  
  
  
int main()  
{  
    /\*level 0\*/  
    Tree t = makeTreeNode(3);  
  
    /\*level 1\*/  
    t->left = makeTreeNode(2);  
    t->right = makeTreeNode(4);  
  
  
    /\*level 2\*/  
    t->left->left = makeTreeNode(2);  
    t->right->left = makeTreeNode(2);  
    t->right->right = makeTreeNode(-1);  
  
    /\*print sum\*/  
    printSum(t, 1);  
    printf("\\n");  
  
    int a\[100\];  
  
    int pos = -1;  
  
    printSum2(t, a, &pos);  
  
    int i;  
  
    for(i = 0; i <= pos; ++i) {  
        printf("%d ", a\[i\]);  
    }  
  
    printf("\\n");  
  
    return 0;  
}  

Nth node in inorder traversal

Problem Here we have to return pointer to nth node in the inorder traversal Solution // Get the pointer to the nth inorder node in "nthnode" void nthinorder(node \*root, int n, mynode \*\*nthnode) { static whichnode; static found; if(!found) { if(root) { nthinorder(root->left, n , nthnode); if(++whichnode == n) { printf("\\nFound %dth node\\n", n); found = 1; \*nthnode = root; // Store the pointer to the nth node. } nthinorder(root->right, n , nthnode); } } } 17\. [Read More]

Print binary search tree in increasing order

Given a binary search tree (aka an “ordered binary tree”), iterate over the nodes to print them out in increasing order. So the tree… 4 / \ 2 5 / \ 1 3 Produces the output “1 2 3 4 5”. This is known as an “inorder” traversal of the tree. Hint: For each node, the strategy is: recur left, print the node data, recur right. Here is code in c/cpp: [Read More]

Threaded binary tree

Since traversing the three is the most frequent operation, a method must be devised to improve the speed. In case of iterative traversal, speed is less than recursive one. So we cans use Threaded tree. If the right link of a node in a tree is NULL, it can be replaced by the address of its inorder successor. This tree is called right in-threaded binary tree. An extra field called the rthread is used. [Read More]