面试过程中手撕代码之二叉树
来源:互联网 发布:机顶盒mac地址绑定 编辑:程序博客网 时间:2024/05/17 20:42
二叉树的个人总结
由于本人目前在找算法工程师方向的工作,在面试过程中,经常会被问到非递归方法遍历的二叉树,二叉搜索树,二叉树的高度等一系列方法,本人写了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);}//非递归先序遍历2vector<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;}//先序遍历3vector<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<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;}//中序遍历2vector<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;}
阅读全文
0 0
- 面试过程中手撕代码之二叉树
- 高效面试之二叉树
- 面试常考手写代码之--二叉树非递归遍历
- 面试常考手写代码之--二叉树层次遍历(BFS)
- 面试常考手写代码之--二叉树层次遍历(BFS)
- 笔试面试之二叉树的深度
- 二叉树的深度 面试之27
- NHN面试之二叉树遍历
- 面试算法之二叉树操作集锦
- 面试算法之二叉树操作集锦
- 面试笔记之二叉树篇
- 面试常考之二叉树
- JAVA面试之二叉树问题
- 面试准备之--二叉树打印
- 面试之常考二叉树
- 面试过程遇到关于二叉树的问题
- c++二叉树构建及面试常见问题代码实现
- 二叉树之三--线索二叉树之代码实现
- 系统安装
- Android连续点击事件的实现
- Java IO流
- 653. Two Sum IV
- rqnoj514 字串距离
- 面试过程中手撕代码之二叉树
- 5-2 使用函数求奇数和
- JavaScript如何获得某一天的前一天日期
- 一、写在前面的话
- Eclipse、object
- 广度优先搜索总结
- ubuntu设置静态IP
- cmd输入命令报错“不是内部或外部命令,也不是可运行的程序或批处理文件”
- 工厂设计模式(三)--抽象工厂模式