面试过程中手撕代码之二叉树
2017-08-31 11:20
197 查看
二叉树的个人总结
由于本人目前在找算法工程师方向的工作,在面试过程中,经常会被问到非递归方法遍历的二叉树,二叉搜索树,二叉树的高度等一系列方法,本人写了c++代码供自己复习使用,希望也能够帮到各位小伙伴们代码块
// // tree.cpp // myTest // // Created by lixiaoxue on 2017/8/28. // Copyright © 2017年 lixiaoxue. All rights reserved. // #include <stdio.h> #include <iostream> #include <queue> #include <vector> #include <algorithm> #include <math.h> #include <numeric> #include <stack> using namespace std; struct TreeNode{ public: int val; TreeNode* left; TreeNode* right; TreeNode(int a): val(a),left(nullptr),right(nullptr){} }; //递归先序遍历 void pre_order(vector<int>& out,TreeNode* root){ if(!root) return; out.push_back(root->val); pre_order(out, root->left); pre_order(out, root->right); } //递归中序遍历 void mid_order(vector<int>& out,TreeNode* root){ if(!root) return; mid_order(out, root->left); out.push_back(root->val); mid_order(out, root->right); } //递归的后序遍历 void after_order(vector<int>& out,TreeNode* root){ if(!root) return; after_order(out, root->left); after_order(out, root->right); out.push_back(root->val); } //非递归先序遍历2 vector<int> pre_order_non2(TreeNode* root){ if(!root) return vector<int>(); vector<int> number; stack<TreeNode*> s; TreeNode* cur = root; while ((!s.empty()) || !cur) { while (cur) { number.push_back(cur->val); s.push(cur); cur = cur->left; } if(!s.empty()){ cur = s.top()->right; s.pop(); } } return number; } //先序遍历3 vector<int> pre_order_non3(TreeNode* root){ if(!root) return vector<int>(); stack<TreeNode*> s; vector<int> number; while (root) { number.push_back(root->val); s.push(root); root = root->left; } while (!s.empty()) { TreeNode* temp = s.top()->right; s.pop(); while (temp) { number.push_back(temp->val); s.push(temp); temp = temp->left; } } return number; } //中序遍历非递归 vector<int> mid_order_non(TreeNode* root){ if(!root) return vector b78f <int>(); stack<TreeNode*> s; vector<int> number; TreeNode* cur = root; while ((!s.empty()) || cur) { while (cur) { s.push(cur); cur = cur->left; } if (!s.empty()) { TreeNode* temp = s.top(); s.pop(); number.push_back(temp->val); cur = temp->right; } } return number; } //中序遍历2 vector<int> mid_order_non2(TreeNode* root){ if(!root) return vector<int>(); stack<TreeNode*> s; vector<int> number; TreeNode* cur = root->left; while ((!s.empty())||cur) { while (cur) { s.push(cur); cur = cur->left; } cur = s.top(); s.pop(); number.push_back(cur->val); cur = cur->right; } return number; } //后序遍历 vector<int> after_order_non(TreeNode* root){ if(!root) return vector<int>(); vector<int> number; stack<TreeNode*> s; TreeNode* cur = root; TreeNode* last = nullptr; while ((!s.empty()) || cur) { while (cur) { s.push(cur); cur = cur->left; } cur = s.top(); if((!cur->right) || cur->right == last){ number.push_back(cur -> val); last = cur; s.pop(); cur = s.top(); }else cur = cur->right; } return number; } void PostOrder_Nonrecursive(TreeNode* root) // 后序遍历的非递归 双栈法 { stack<TreeNode*> s1 , s2; TreeNode* curr ; // 指向当前要检查的节点 s1.push(root); while(!s1.empty()) // 栈空时结束 { curr = s1.top(); s1.pop(); s2.push(curr); if(curr->left) s1.push(curr->left); if(curr->right) s1.push(curr->right); } while(!s2.empty()) { printf("%c ", s2.top()->val); s2.pop(); } } //求二叉树非遍历情况下的平均深度 int findDepth(TreeNode* root){ if(!root) return 0; root->val = 1; vector<int> height; vector<TreeNode*> tree{root}; while (tree.size()) { root = tree.back(); tree.pop_back(); if(!root->left && !root->right) height.push_back(root->val); if(root->left){ tree.push_back(root->left); root->left->val = root->val+1; } if(root->right){ tree.push_back(root->right); root->right->val = root->val + 1; } } int sum = accumulate(height.begin(), height.end(), 0); cout << sum << " " << height.size() << endl; return (float)sum/(float)height.size(); } //求二叉树非遍历情况下的平均深度 int tree_depth(TreeNode* root){ if(!root) return 0; vector<TreeNode*> temp{root}; int h =0; vector<int> height; while (temp.size()) { h+= 1; vector<TreeNode*> nei; for(TreeNode* it:temp){ if(!it->left && !it->right) {height.push_back(h);continue;} if(it->left) nei.push_back(it->left); if (it->right) { nei.push_back(it->right); } } temp = nei; } int sum= accumulate(height.begin(), height.end(), 0); cout << sum << " " << height.size() << endl; return sum/height.size(); } int main(){ TreeNode* root = new TreeNode(6); TreeNode* left1 = new TreeNode(6); TreeNode* right1 = new TreeNode(6); TreeNode* left12 = new TreeNode(6); TreeNode* left123 = new TreeNode(6); TreeNode* left1234 = new TreeNode(6); root->left = left1; left1->left = left12; left12->left = left123; left123->right = left1234; root->right = right1; int zhi = tree_depth(root); cout << zhi << endl; return 0; }
相关文章推荐
- 面试常考手写代码之--二叉树非递归遍历
- 面试常考手写代码之--二叉树层次遍历(BFS)
- 面试过程遇到关于二叉树的问题
- 软件开发者面试百问-----描述一下实现一段代码的过程,从需求到最终交付
- 面试常考手写代码之--二叉树层次遍历(BFS)
- 【面试必备】手撕代码,你怕不怕?
- 面试过程中,常见大坑!
- 写代码过程中必须有的几个本能
- 编译原理程序设计实践(六) 语句和子过程的处理代码
- MPEG2代码分析Part1 初始化过程与外部框架
- 轻松搞定面试中的二叉树题目
- javascript实现动态模态绑定grid过程代码
- 数据结构面试之六——二叉树的常见操作2(非递归遍历&二叉排序树)
- 主题: 今天面试一牛人,来欣赏欣赏牛人的代码
- Android面试过程描写叙述
- 坏了,我写不出来面向过程的代码了 -- 面向对象之订单状态改变的设计
- 编程面试过程中常见的10大算法
- SQLServer存储过程创建和修改的实现代码
- 从头说12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- 复旦、交大“综合评价”面试今结束,详解两校面试全过程