师兄面试总结编程部分解答之四
2014-08-27 20:30
225 查看
个人感觉树的问题大部分都是用递归的思想。
首先定义树节点结构体
构造搜索二叉树:
1、树的深度,广度遍历源代码
2、二叉树镜像(剑指offer125页)
3、二叉树中某一值的路径(剑指offer 143页)
4、二叉搜索树后序遍历序列(剑指offer 140页)
首先定义树节点结构体
typedef struct _treeNode { int val; _treeNode* left; _treeNode* right; _treeNode (int value) { val = value; left = NULL; right = NULL; } }TreeNode,*TreeNodePtr;
构造搜索二叉树:
//根据输入内容构造二叉查找 TreeNodePtr constructTree() { int c; TreeNodePtr parentNode,root,curNode; std::cin>>c; root = new TreeNode(c); parentNode = root; curNode = root; while((std::cin>>c)) { curNode = root; while(curNode) { if (c < curNode->val) { parentNode = curNode; curNode = curNode->left; }else { parentNode = curNode; curNode = curNode->right; } } TreeNodePtr tmpNode = new TreeNode(c); if(c < parentNode->val) parentNode->left = tmpNode; else parentNode->right = tmpNode; } return root; }
1、树的深度,广度遍历源代码
//使用堆栈 //深度优先遍历,非递归算法实现 //代码书写熟练程度远远不够,还要进行多次调试,说明思路不清晰 void DFS(TreeNodePtr root) { if(root == NULL) return; TreeNodePtr stack[MAX]; int top = 0; TreeNodePtr cur = root; stack[top++] = cur; cur = cur->left; while(top != 0 || cur != NULL) { while(cur != NULL) { stack[top++] = cur; cur = cur->left; } if(top > 0) { cur = stack[--top]; std::cout<<cur->val<<" "; cur = cur->right; } } std::cout<<std::endl; } //使用队列 //广度优先遍历 void BFS(TreeNodePtr root) { if (root == NULL) return; TreeNodePtr queue[MAX]; int head = 0; int tail = 0; queue[head++] = root; TreeNodePtr cur; while(tail != head) { cur = queue[tail++]; std::cout<<cur->val<<" "; if(cur->left) queue[head++] = cur->left; if(cur->right) queue[head++] = cur->right; } std::cout<<std::endl; }
2、二叉树镜像(剑指offer125页)
//将给定树,转化为其镜像,左右互换 void change(TreeNodePtr root) { if(root == NULL) return; TreeNodePtr tmp = root->left; root->left = root->right; root->right = tmp; } void TreeMirror(TreeNodePtr root) { //思路利用广度优先遍历,并分别互换子树 if(root == NULL) return; TreeNodePtr queue[MAX]; int head = 0; int tail = 0; queue[head++] = root; TreeNodePtr cur; while(tail != head) { cur = queue[tail++]; change(cur); if(cur->left) queue[head++] = cur->left; if(cur->right) queue[head++] = cur->right; } } //二叉树镜像递归实现 void TreeMirror_recu(TreeNodePtr root) { if (root == NULL) return; TreeNodePtr tmp = root->left; root->left = root->right; root->right = tmp; TreeMirror_recu(root->left); TreeMirror_recu(root->right); }
3、二叉树中某一值的路径(剑指offer 143页)
//在二叉树中找出和为某一值的所有路径 //找到路径还是比较简单的,关键是打印路径怎么去实现 bool isLeaf(TreeNodePtr root) { if (!root) return false; if(root->left == NULL && root->right == NULL) return true; else return false; } void findPath(TreeNodePtr root, int sum,TreeNodePtr stack[],int top) { if(root == NULL) return; if(isLeaf(root) && root->val == sum) { std::cout<<"find a path: "; for(int i = 0; i < top;i++) { std::cout<<stack[i]->val<<" "; } std::cout<<root->val; std::cout<<std::endl; return; } if( !isLeaf(root) && root->val < sum) { stack[top++] = root; if (root->left) findPath(root->left, sum - root->val,stack,top); if(root->right) findPath(root->right, sum - root->val,stack,top); } else return; }
4、二叉搜索树后序遍历序列(剑指offer 140页)
//后序遍历,这里的顺序是只根节点的遍历顺序,后序即为 左子树-右子树-根节点 顺序完成遍历 void LRD(TreeNodePtr root) { if (!root) return; TreeNodePtr stack[MAX]; int record[MAX] = {0}; int top = 0; TreeNodePtr cur = root; while(top != 0 || cur != NULL) { while(cur != NULL)//左支深度遍历 { stack[top] = cur; record[top++] = 1; cur = cur->left; } cur = stack[top-1]; if(cur->right == NULL || record[top - 1] == 2)//当前节点右子树为空,或已经二次遍历 { std::cout<<cur->val<<" "; top--; cur = NULL; } else//遍历右子树 { record[top - 1] = 2; cur = cur->right; } } }
相关文章推荐
- 师兄面试总结编程部分解答之二
- 师兄面试总结编程部分解答之五
- 师兄面试总结编程部分解答之一
- 师兄面试总结编程部分解答之三
- 系统编程部分知识点总结
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- Matlab 与 VC++ 混合编程过程详解(含问题部分解答)
- 【转】[IT综合面试]牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- 关于09年迅雷面试+笔试+上机题目的总结(解答来自网络)
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 《linux c 编程一站式学习》课后部分习题解答
- c++服务器编程面试总结
- erlang基础练习-编程指南习题部分解答
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- 【经验总结】服务器端编程部分概念理解
- 个人项目编程的部分总结1
- perl编程的部分问题总结