二叉树的个人总结
由于本人目前在找算法工程师方向的工作,在面试过程中,经常会被问到非递归方法遍历的二叉树,二叉搜索树,二叉树的高度等一系列方法,本人写了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;