面试过程中手撕代码之二叉树

本文是作者对二叉树的个人总结,特别关注面试中常见的非递归遍历、二叉搜索树和计算二叉树高度等问题。作者分享了用C++编写的代码,旨在帮助准备算法工程师面试的读者进行复习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的个人总结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值