字节跳动测开面试手撕代码汇总(c++)

树相关
树的非递归遍历

#include
#include
#include
struct TreeNode{
   
    int val;
    TreeNode *l;
    TreeNode *r;
};
TreeNode* createtree(){
   
    TreeNode *root;
    int a[]={
   1,2,4,0,0,0,3,5,0,0,6,0,0};
    static int  t=0;
    root=new TreeNode;
    int data;
    data=a[t++];
    if(data==0){
   
        return NULL;
    }
    root->val=data;
    root->l=createtree();
    root->r=createtree();
    return root;
}

//非递归前序遍历
void preorder(TreeNode *root )
{
   

    stack<TreeNode *> s;
    TreeNode *p = root;
    while(p != NULL || !s.empty())
    {
   
        while(p != NULL)
        {
   
            cout<<p->val;
            s.push(p);
            p = p->l;
        }
        if(!s.empty())
        {
   
            p = s.top();
            s.pop();
            p = p->r;
        }
    }
}

//非递归中序遍历
void inorder(TreeNode *root)
{
   
    stack<TreeNode *> s;
    TreeNode *p = root;
    while(p != NULL || !s.empty())
    {
   
        while(p != NULL)
        {
   
            s.push(p);
            p = p->l;
        }
        if(!s.empty())
        {
   
            p = s.top();
            cout<<p->val;
            s.pop();
            p = p->r;
        }
    }
}

//更简单的非递归后序遍历
void postorder(TreeNode *root)//局部后序,全部后序
{
   //将栈顶元素取出,使以此元素为“根”结点的局部有序入栈,但若此前已通过该结点将其局部入栈,则直接出栈输出即可。
    stack< pair<TreeNode *, bool> > s;
    s.push(make_pair(root, false));
    bool visited;
    while(!s.empty())
    {
   
        root = s.top().first;
        visited = s.top().second;
        s.pop();
        if(root == NULL)
            continue;
        if(visited)
        {
   
            cout<<root->val;
        }
        else
        {
   
            s.push(make_pair(root, true));
            s.push(make_pair(root->r, false));
            s.push(make_pair(root->l, false));
        }
    }
}
int main() {
   
    TreeNode * root;
    root=createtree();
   // preorder(root);
   // inorder(root);
    postorder(root);
    return 0;

}

二叉树的最大距离
两种情况:
情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。
情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。

#include
#include
using namespace std;
struct TreeNode{
   
    char val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x):val(x),left(NULL),right(NULL){
   };
};
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
   
    if(preorder.size()==0||inorder.size()==0){
   
        return NULL;
    }
    TreeNode* tree=new TreeNode(preorder[0]);//先序的第一个节点树根
    int mid=distance(inorder.begin(),find(inorder.begin(),inorder.end(),preorder[0]));
    vector<int> pre_l(preorder.begin()+1,preorder.begin()+mid+1);
    vector<int>pre_r(preorder.begin()+mid+1,preorder.end());
    vector<int> in_l(inorder.begin(),inorder.begin()+mid);
    vector<int>in_r(inorder.begin()+mid+1,inorder.end());
    tree->left=buildTree(pre_l,in_l);
    tree->right=buildTree(pre_r,in_r);
    return tree;

}

int treedistance(TreeNode *root,int &maxdis){
   //返回树的深度
    if(root== nullptr){
   //根深度为-1
        return -1;
    }
    int leftdis=treedistance(root->left,maxdis)+1;//左边的距离
    int rightdis=treedistance(root->right,maxdis)+1;
    int dis=leftdis+rightdis;//每次的距离
    maxdis=dis>maxdis?dis:maxdis;//返回最大距离
    return leftdis>rightdis?leftdis:rightdis;

}

int main(){
   
    vector<int> preorder={
   1,2,4,7,3,5,6,8};
    vector<int> inorder={
   4,7,2,1,5,3,8,6};
    TreeNode *root;
    int maxdis=0;
    cout<<treedistance(buildTree(preorder,inorder),maxdis)<<endl;
    cout<<maxdis;
    return 0;

}

3.字节跳动测开面试手撕代码汇总(c++)_第1张图片

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
   
public:
vector<int> arr;
        vector<vector<int>> matrix;
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
   
        
        dfs(root,sum);
        return matrix;
        
    }
    void dfs(TreeNode* root,int sum){
   
        if(!root){
   
            return ;
        }
        arr.push_back(root->val

你可能感兴趣的:(算法,算法)