• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ countNodes函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中countNodes函数的典型用法代码示例。如果您正苦于以下问题:C++ countNodes函数的具体用法?C++ countNodes怎么用?C++ countNodes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了countNodes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: countNodes

 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;
 }
开发者ID:zhuhuijia0001,项目名称:leetcode,代码行数:30,代码来源:222-Count+Complete+Tree+Nodes.cpp


示例2: countNodes

/**
 * 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);
}
开发者ID:SumatoAppy,项目名称:leetcode-1,代码行数:34,代码来源:count-complete-tree-nodes.c


示例3: deleteAtPosition

//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;                    
             }                          
         }
     }      
开发者ID:lodhaakash07,项目名称:Data-Structures-And-Algorithm,代码行数:32,代码来源:deletingNodes.cpp


示例4: countNodes

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);
    
}
开发者ID:YaoZengzeng,项目名称:leetcode,代码行数:25,代码来源:Count+Complete+Tree+Nodes.c


示例5: countNodes

 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;
     }
 }
开发者ID:syjohnson,项目名称:Leetcode,代码行数:27,代码来源:222.cpp


示例6: countNodes

 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;
     }
 
 }
开发者ID:nothingbutu,项目名称:LeetCode,代码行数:25,代码来源:Count_Nodes.cpp


示例7: countNodes

 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);
         }
     }
 }
开发者ID:qiaoyiX,项目名称:leetcode,代码行数:25,代码来源:CountCompleteTreeNodes.cpp


示例8: countNodes

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);
}
开发者ID:viing937,项目名称:leetcode,代码行数:25,代码来源:222.c


示例9: countNodes

 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;
 }
开发者ID:0-1heyi,项目名称:leetcode,代码行数:7,代码来源:CountCompleteTreeNodes.cpp


示例10: countNodes

 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;
 }
开发者ID:psc0606,项目名称:algorithm,代码行数:7,代码来源:011CountCompleteTreeNodes.cpp


示例11: countNodes

/* 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; 
} 
开发者ID:KunjeshBaghel,项目名称:DS,代码行数:8,代码来源:BT_to_BST_g4g.cpp


示例12: getIntersectionNode

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);
}
开发者ID:amit-upadhyay-IT,项目名称:probable-octo-disco,代码行数:8,代码来源:06_4_1.c


示例13: countNodes

 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);
 }
开发者ID:chenx,项目名称:oj,代码行数:8,代码来源:CountCompleteTreeNodes.cpp


示例14: countNodes

 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);
 }
开发者ID:SccsAtmtn,项目名称:leetcode,代码行数:9,代码来源:222.cpp


示例15: countNodes

 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);
 }
开发者ID:lilmuggle,项目名称:leetcode,代码行数:9,代码来源:countCBTnodes.cpp


示例16: nodevisited

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;
}
开发者ID:DrSobik,项目名称:IPPS,代码行数:57,代码来源:ProcessModel.cpp


示例17: countNodes

/**
 * 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);
 }
开发者ID:yangjin-unique,项目名称:leetcode,代码行数:17,代码来源:KthSmallestElementinaBST.c


示例18: countNodes

int countNodes(TreeNode T) {
	int Nodes = 0;
	
	if (T != NULL) {
		Nodes++;
		Nodes += countNodes(T->Left) + countNodes(T->Right);
	}
	return Nodes;
}
开发者ID:l-iberty,项目名称:MyCode,代码行数:9,代码来源:Counters.c


示例19: countNodes

 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;
 }
开发者ID:heshenghuan,项目名称:leetcode-exercise,代码行数:10,代码来源:222_Count_Complete_Tree_Nodes.cpp


示例20: countNodes

 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;
 }
开发者ID:TakuyaKimura,项目名称:Leetcode,代码行数:10,代码来源:222.cpp



注:本文中的countNodes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ count_bits函数代码示例发布时间:2022-05-30
下一篇:
C++ countChanged函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap