int countNodes(TreeNode* root) {
     
     if (root == nullptr) {
         return 0;
     }
     
     TreeNode *left = root->left;
     TreeNode *right = root->right;
     
     int leftCount = 1;
     int rightCount = 1;
     
     while (left != nullptr) {
         leftCount++;
         
         left = left->left;
     }
     
     while (right != nullptr) {
         rightCount++;
         
         right = right->right;
     }
     
     if (leftCount == rightCount) {
         return pow(2, leftCount) - 1;    
     }
     
     return countNodes(root->left) + countNodes(root->right) + 1;
 }
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int countNodes(struct TreeNode* root) {
    if(root == NULL)
        return 0;

    int lh,rh;
    struct TreeNode *tmp;

    tmp = root->left;
    lh = 0;
    while(tmp){
        lh++;
        tmp = tmp->left;
    }

    tmp = root->right;
    rh = 0;
    while(tmp){
        rh++;
        tmp = tmp->right;
    }

    if(lh == rh)
        return (2<<lh) - 1;

    return 1 + countNodes(root->left) + countNodes(root->right);
}
//fUNCTION TO DELETE AT SPECIFIC PISTION
void deleteAtPosition(int position)
     {
     //First check if the position exsists
     //countNodes() returns the total number of nodes                      
     if(position>countNodes())
         printf("Enter valid Position");           
     else                     
         {
         //delete the first node                     
         if(position==1)
                deleteFirstNode();
         //delete last node       
         else if(position==countNodes())
                deleteLastNode();
         else
             {
             struct node *temp,*curr=head,*prev=curr;   
             /*traverse to the position and delete the node
                        prev contains all the elements before the delete position
                        curr conatins all the position after the delete position
             */
             for(int i=1;i<position;i++)
                   {
                   prev = curr;                       
                   curr = curr->next;                   
                   }       
             prev->next = NULL;
             prev->next = curr->next;                    
             }                          
         }
     }      
int countNodes(struct TreeNode* root) {
    int l=0,r=0;
    struct TreeNode* p;
    if(root==NULL){
        return 0;
    }
    
    p=root;
    while(p){
        l++;
        p=p->left;
    }
    p=root;
    while(p){
        r++;
        p=p->right;
    }
    
    if(l==r){
        return (1<<l)-1;
    }
    
    return 1+countNodes(root->left)+countNodes(root->right);
    
}
예제 #5
0
파일: 222.cpp 프로젝트: syjohnson/Leetcode
 int countNodes(TreeNode* root) {
 	// check exception
 	if (!root) {
 		return 0;
 	}
     TreeNode* l = root;
     TreeNode* r = root;
     int leftDepth = 0;
     int rightDepth = 0;
     while (l) {
     	leftDepth++;
     	l = l->left;
     }
     while (r) {
     	rightDepth++;
     	r = r->right;
     }
     if (leftDepth == rightDepth) {
     	// return (1 << leftDepth) - 1;
     	return pow(2, leftDepth) - 1;
     } else {
     	// wrong 
     	// return countNodes(l) + countNodes(r) + 1;
     	// should be 
     	return countNodes(root->left) + countNodes(root->right) + 1;
     }
 }
예제 #6
0
 int countNodes(TreeNode* root) {
     if(root==NULL){
         return 0;
     }
     
     int leftDepth=1,rightDepth=1;
     TreeNode* p=NULL;
     p=root->left;
     while(p!=NULL){
         leftDepth++;
         p=p->left;
     }
     p=root->right;
     while(p!=NULL){
         rightDepth++;
         p=p->right;
     }
     
     if(leftDepth==rightDepth){
         return (1<<leftDepth)-1;
     }else{
         return countNodes(root->left)+countNodes(root->right)+1;
     }
 
 }
예제 #7
0
 int countNodes(TreeNode* root) {
     if(!root) {
         return 0;
     }
     else {
         TreeNode* leftTree = root;
         TreeNode* rightTree = root;
         int lefth = 0;
         int righth = 0;
         while(leftTree) {
             leftTree = leftTree->left;
             lefth++;
         }
         while(rightTree) {
             rightTree = rightTree->right;
             righth++;
         }
         if(lefth == righth) {
             return (int)pow(2, lefth) - 1;
         }
         else {
             return 1 + countNodes(root->left) + countNodes(root->right);
         }
     }
 }
예제 #8
0
파일: 222.c 프로젝트: viing937/leetcode
int countNodes(struct TreeNode* root)
{
    if( root == NULL )
        return 0;

    int heightL = 0, heightR = 0;
    struct TreeNode *p;
    p = root;
    while( p != NULL )
    {
        heightL++;
        p = p->left;
    } 

    p = root;
    while( p != NULL )
    {
        heightR++;
        p = p->right;
    }

    if( heightL == heightR )
        return (1<<heightL)-1;
    return 1 + countNodes(root->left)+countNodes(root->right);
}
예제 #9
0
 int countNodes(TreeNode* root) {
     int cnt = isCompleteTree(root);
     if (cnt != -1) return cnt;
     int leftCnt = countNodes(root->left);
     int rightCnt = countNodes(root->right);
     return leftCnt + rightCnt + 1;
 }
 int countNodes(TreeNode* root) {
     int tmp = isComplete(root);
     if (tmp != -1) return tmp;
     int l = countNodes(root->left);
     int r = countNodes(root->right);
     return 1 + l + r;
 }
예제 #11
0
/* A helper function to count nodes in a Binary Tree */
int countNodes (struct node* root) 
{ 
  if (root == NULL) 
   return 0; 
  return countNodes (root->left) + 
         countNodes (root->right) + 1; 
} 
예제 #12
0
int getIntersectionNode(struct node *head1, struct node *head2)
{
	int l1 = countNodes(head1);
	int l2 = countNodes(head2);
	int diff = abs(l1 - l2);
	return (l1 > l2)? _getIntersectionNode(diff, head1, head2):
	_getIntersectionNode(diff, head2, head1);
}
예제 #13
0
 int countNodes(TreeNode* root) {
     if (root == NULL) return 0;
     int L = 0, R = 0;
     for (TreeNode * n = root->left; n != NULL; n = n->left) ++ L;
     for (TreeNode * n = root->right; n != NULL; n = n->right) ++ R;
     if (L == R) return pow(2, L+1) - 1;
     else return 1 + countNodes(root->left) + countNodes(root->right);
 }
예제 #14
0
파일: 222.cpp 프로젝트: SccsAtmtn/leetcode
 int countNodes(TreeNode* root) {
     if (!root) return 0;
     int leftHeight = count(root->left);
     int rightHeight = count(root->right);
     if (leftHeight==rightHeight)
         return (1<<leftHeight)+countNodes(root->right);
     else
         return (1<<rightHeight)+countNodes(root->left);
 }
예제 #15
0
 int countNodes(TreeNode* root) {
     if(!root)
         return 0;
     vector<int> level = helper(root->left);
     if(level[0] == level[1])
         return pow(2, level[0]) + countNodes(root->right);
     else
         return pow(2, level[1]) + countNodes(root->left);
 }
예제 #16
0
QList<ListDigraph::Node> ProcessModel::topolSortReachableFrom(const QList<ListDigraph::Node>& s) {
	ListDigraph::NodeMap<bool > nodevisited(graph, false);
	QList<ListDigraph::Node> res;
	QStack<ListDigraph::Node> stack;
	ListDigraph::Node curnode;
	ListDigraph::Node pnode;
	ListDigraph::Node snode;
	QList<ListDigraph::Node> reachable = reachableFrom(s);

	// Reserve memory
	res.reserve(countNodes(graph));
	stack.reserve(countNodes(graph));

	for (int i = 0; i < s.size(); i++) {
		if (s[i] != INVALID) {
			stack.push(s[i]);
		}
	}

	bool psched;
	while (!stack.empty()) {
		curnode = stack.pop();

		if (!nodevisited[curnode]) { // This node has not been visited yet

			// Check whether all predecessors in reachable are scheduled
			psched = true;
			for (ListDigraph::InArcIt iait(graph, curnode); iait != INVALID; ++iait) {
				pnode = graph.source(iait);
				if (reachable.contains(pnode)) { // Consider only nodes which can be reached from s
					if (!nodevisited[pnode]) {
						psched = false;
						break;
					}
				}
			}

			if (psched) { // All predecessors have been visited
				res.append(curnode);
				nodevisited[curnode] = true;

				// Push the succeeding nodes
				for (ListDigraph::OutArcIt oait(graph, curnode); oait != INVALID; ++oait) {
					snode = graph.target(oait);
					if (!nodevisited[snode]) {
						stack.push(snode);
					}
				}
			} else {
				stack.prepend(curnode);
			}

		} // Else ignore the visited node
	}

	return res;
}
/**
 * method 2: using properties of bst.
 * if number of nodes in left-subtree greater than (k-1), then the k-th smallest 
 * must be in left-rubtree;
 * if number less than (k-1), then k-th node must be in right-subtree.
 * if equal, root is the one
 * Time: averag o(logn)
 */
int countNodes(struct TreeNode *root)
 {
 	int nl = 0, nr = 0;

 	if (root == NULL) return 0;
 	nl = countNodes(root->left);
 	nr = countNodes(root->right);
 	return (nl + nr + 1);
 }
예제 #18
0
파일: Counters.c 프로젝트: l-iberty/MyCode
int countNodes(TreeNode T) {
	int Nodes = 0;
	
	if (T != NULL) {
		Nodes++;
		Nodes += countNodes(T->Left) + countNodes(T->Right);
	}
	return Nodes;
}
 int countNodes(TreeNode* root) {
     if(!root) return 0;
     
     int l = getLeftDepth(root);
     int r = getRightDepth(root);
     
     if(l == r) return (2<<(l-1)) - 1;
     
     return countNodes(root->left) + countNodes(root->right) + 1;
 }
예제 #20
0
 int countNodes(TreeNode* root) {
     int leftHeight = 0, rightHeight = 0;
     for (TreeNode *cur = root; cur; cur = cur->left)
         ++leftHeight;
     for (TreeNode *cur = root; cur; cur = cur->right)
         ++rightHeight;
     if (leftHeight == rightHeight)
         return (1 << leftHeight) - 1;
     return countNodes(root->left) + countNodes(root->right) + 1;
 }
예제 #21
0
le * evaluateDefun( lithp_burrito *lb, le * fcn, le * params )
{
    le * function;
    le * thisparam;
    le * result;
    int count;

    /* make sure both lists exist */
    if (!fcn)  return( leNew( "NIL" ));

    /* check for the correct number of parameters */
    if (countNodes(fcn->branch) > countNodes(params))
        return( leNew( "NIL" ));

    /* allocate another function definition, since we're gonna hack it */
    function = leDup(fcn);

    /* pass 1:  tag each node properly.
    	    for each parameter: (fcn)
    	    - look for it in the tree, tag those with the value
    */
    count = 0;
    thisparam = fcn->branch;
    while (thisparam)
    {
        leTagData(function, thisparam->data, count);
        thisparam = thisparam->list_next;
        count++;
    }

    /* pass 2:  replace
    	    for each parameter: (param)
    	    - evaluate the passed in value
    	    - replace it in the tree
    */
    count = 0;
    thisparam = params;
    while (thisparam)
    {
        result = evaluateNode( lb, thisparam );
        leTagReplace(function, count, result);
        thisparam = thisparam->list_next;
        leWipe(result);
        count++;
    }

    /* then evaluate the resulting tree */
    result = evaluateBranch( lb, function->list_next );

    /* free any space allocated */
    leWipe( function );

    /* return the evaluation */
    return( result );
}
int countNodes(TreeNode* root) {
	if( root == NULL)
		return 0;
	int l = getLeft(root) +1;
	int r = getRight(root) +1;

	if( l == r )
		return (2<<(l-1)) -1;
	else
		return countNodes(root->left) +countNodes(root->right) +1;
}
  int countNodes(TreeNode *root) {
    if (root == NULL)
      return 0;
    int lDepth = getDepth(root->left);
    int rDepth = getDepth(root->right);

    if (lDepth == rDepth)
      return (1<<lDepth) + countNodes(root->right);
    else
      return (1<<rDepth) + countNodes(root->left);
  }
예제 #24
0
int TCBinaryObjectTree::countNodes(TCBinaryObjectTreeNode *node)
{
	if (node)
	{
		return countNodes(node->left) + countNodes(node->right) + 1;
	}
	else
	{
		return 0;
	}
}
예제 #25
0
 int countNodes(TreeNode* root) {
     if (root == NULL) return 0;
     int lh = 0;
     int rh = 0;
     TreeNode* node = root;
     while (node != NULL) { lh++; node = node->left; }
     node = root;
     while (node != NULL) { rh++; node = node->right; }
     if (lh == rh) { return pow(2, lh) - 1; }
     return 1 + countNodes(root->left) + countNodes(root->right);
 }
예제 #26
0
 int countNodes(TreeNode* root) {
     if(root == NULL) return 0;
     if(root->left == NULL && root->right == NULL) return 1;
     int l = getLeftDepth(root);
     int r = getRightDepth(root);
     if(l == r) {
         return (1<<l) - 1;
     } else {
         return 1 + countNodes(root->left) + countNodes(root->right);
     }
 }
예제 #27
0
 int countNodes(TreeNode* root) {
     if( root == NULL) return 0;
     int lh = 0, rh = 0;
     lh = get_left_hight(root);
     rh = get_right_hight(root);
     
     if(lh == rh) return (1<<(lh)) -1;
     
     else return 1 + countNodes(root->left) + countNodes(root->right);
     
     
 }
	int countNodes(TreeNode* root) {
		if (root == NULL)
			return 0;
		int leftHeigth = lheight(root->left);
		int rightHeight = rheight(root->right);
		if (leftHeigth == rightHeight) //可以快速判断是满完全二叉树
			return pow(2, leftHeigth + 1) - 1;
		else// if (leftHeigth > rightHeight){ //并非满完全二叉树
			return countNodes(root->left) + countNodes(root->right) + 1;
		//}
		//else if (leftHeigth < rightHeight) //对于完全二叉树而言,不会出现这种情况
	}
예제 #29
0
파일: 222-AC.cpp 프로젝트: jeffchy/jiangchy
 int countNodes(TreeNode* root) {
     int left_depth = countLeft(root);
     int right_depth = countRight(root);
     
     if (left_depth == right_depth){
         return ((1 << (left_depth+1)) - 1);
     }
     else{
         return countNodes(root->left)+countNodes(root->right)+1;
     }
     
 }
 int countNodes(TreeNode* root) {
     if (!root)
         return 0;
     int leftDepth = 0, rightDepth= 0;
     for(TreeNode* p=root; p; p=p->left) ++leftDepth;
     for(TreeNode* p=root; p; p=p->right) ++rightDepth;
     if (leftDepth==rightDepth) {
         return (1<< leftDepth) - 1 ;
     }
     else {
         return countNodes(root->left) + countNodes(root->right) + 1 ;
     }
 }