Posts

Showing posts from August, 2020

sum of left leaves in a tree(leetcode).

 sum of left leaves in a tree(leetcode).  class Solution { public:               int sumOfLeftLeaves(TreeNode* root)     {         queue<TreeNode *>q;         int sum=0;         if(root==NULL)             return 0;         else if(root->left==NULL && root->right==NULL)             return 0;         else         {                 q.push(root);         while(!q.empty())         {             Tre...
 sorting a array with 0,1,2 elements only.   void sort_0_1_2(int a[], int n) {     // sort(a,a+n); //direct function     int low,mid,high;     low=mid=0;     high=n-1;     while(mid<=high)     {         if(a[mid]==0)           {               swap(a[mid],a[low]);           low++;           mid++;           }           else if(a[mid]==2)           {               swap(a[mid],a[high]);             ...

inorder traversal without using recursion

  inorder traversal  without using recursion vector<int> inOrder(Node* root) {   vector<int>v;   stack<Node*>st;         while(root!=NULL  ||  !st.empty())   {       while(root)       {           st.push(root);           root=root->left;                  }       root=st.top();       st.pop();              v.push_back(root->data);              root=root->right;          }   return v;   }