上一页 1 2 3 4 5 6 7 8 9 10 ··· 24 下一页
摘要: 可以通过求第K层的结点,但是反复递归时间复杂度很高可以通过队列来实现void LevelTraverse(BiTNode * pRoot) { if(pRoot == NULL) return; queue q; q.push(pRoot); while(!q.empty()) { BiTNode * pNode = q.front(); q.pop(); coutdata lchild != NULL) q.push(pNode->lchild); if(pNode->rchild != NULL) q.push(pNode... 阅读全文
posted @ 2013-08-27 23:59 l851654152 阅读(242) 评论(0) 推荐(0) 编辑
摘要: int GetNodeNumKthLevel(BiTNode * pRoot, int k) { if(pRoot == NULL || k lchild, k-1); // 左子树中k-1层的节点个数 int numRight = GetNodeNumKthLevel(pRoot->rchild, k-1); // 右子树中k-1层的节点个数 return (numLeft + numRight); } 阅读全文
posted @ 2013-08-27 22:36 l851654152 阅读(226) 评论(0) 推荐(0) 编辑
摘要: bool strcutcmp(BiTNode *pRoot1,BiTNode *pRoot2){ if (pRoot1 == NULL && pRoot2 == NULL) { return true; } if (pRoot1 == NULL || pRoot2 ==NULL) { return false; } bool left = strcutcmp(pRoot1->lchild,pRoot2->lchild); bool right = strcutcmp(pRoot1->rchild,pRoot2->rchild); return left 阅读全文
posted @ 2013-08-27 20:15 l851654152 阅读(201) 评论(0) 推荐(0) 编辑
摘要: int GetLeafNodeNumber(BiTNode *T){ if (T == NULL) { return 0; } if (T->lchild == NULL && T->rchild == NULL) { return 1; } return GetLeafNodeNumber(T->lchild) + GetLeafNodeNumber(T->rchild);} 阅读全文
posted @ 2013-08-27 01:58 l851654152 阅读(461) 评论(0) 推荐(0) 编辑
摘要: int GetLength(BiTNode* T){ if (T == NULL) { return 0; } int leftlegth = GetLength(T->lchild); int rightlegth = GetLength(T->rchild); return leftlegth > rightlegth ? leftlegth + 1:rightlegth +1;} 阅读全文
posted @ 2013-08-27 01:46 l851654152 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 1.按照先序创建二叉树void Create(BiTree& pRoot){ char ch; cin >> ch; if (ch == '#') { pRoot = NULL; } else { pRoot = (BiTNode*)malloc(sizeof(BiTNode)); pRoot->data = ch; Create(pRoot->lchild); Create(pRoot->rchild); }}2.先序遍历二叉树void PreRoot(BiTNode* T){ if (T != NULL) { cout data lch 阅读全文
posted @ 2013-08-27 01:15 l851654152 阅读(135) 评论(0) 推荐(0) 编辑
摘要: (1)如果二叉树为空,节点个数为0(2)如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1int GetNodeNumber(BiTNode* T){ if (T == NULL) { return 0; } else return GetNodeNumber(T->lchild) + GetNodeNumber(T->rchild) + 1;} 阅读全文
posted @ 2013-08-27 00:11 l851654152 阅读(887) 评论(0) 推荐(0) 编辑
摘要: 1.数组求和如果只是普通求和会简单,但是只能使用一行代码该怎么办呢?int sum(int *a,int n){ return n ==0 ? 0 : sum(a,n-1) + a[n-1]; }2.寻找发帖水王int Find(int *a,int n){ int times = 0; int value; for (int i = 0;i max) { max = a[i]; } if (a[i] max) { max = a[i]; } if (a[i] sum) { ahead--; } else { behind++; } }}int main... 阅读全文
posted @ 2013-08-26 13:01 l851654152 阅读(314) 评论(0) 推荐(0) 编辑
摘要: 可以利用额外的空间则可以遍历两次时间复杂度O(N) 空间复杂度O(1)void Translate(int a[], int b[], int n){ b[0] = 1; for (int i = 1; i = 1; i--) { b[i] *= b[0]; b[0] *= a[i]; }} 阅读全文
posted @ 2013-08-26 12:53 l851654152 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 2 阅读全文
posted @ 2013-08-26 02:04 l851654152 阅读(139) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 10 ··· 24 下一页