首页 > 其他分享 >代码随想录day14 LeetCode 144.二叉树的前序遍历 145.二叉树的后序遍历 94.二叉树的中序遍历 二叉树的链式存储结构创建

代码随想录day14 LeetCode 144.二叉树的前序遍历 145.二叉树的后序遍历 94.二叉树的中序遍历 二叉树的链式存储结构创建

时间:2023-01-10 17:14:06浏览次数:69  
标签:遍历 随想录 BiTreeNode tq 二叉树 NULL root

144. 二叉树的前序遍历

class Solution {
public:
    vector<int> v;
    vector<int> preorderTraversal(TreeNode* root) {
    
        if(root==NULL)return v;
        v.push_back(root->val);
        preorderTraversal(root->left);
        preorderTraversal(root->right);
        return v;
    }
};

145. 二叉树的后序遍历

class Solution {
public:
    vector<int>result;
    vector<int> postorderTraversal(TreeNode* root) {
        if(root==NULL)return result;
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        result.push_back(root->val);
        return result;
    }
};

94. 二叉树的中序遍历

class Solution {
public:
    vector<int>result;
    vector<int> inorderTraversal(TreeNode* root) {
        if(root==NULL)return result;
        inorderTraversal(root->left);
        result.push_back(root->val);
        inorderTraversal(root->right);
        return result;
    }
};
class BiTreeNode {
public:
    char  data;                    //数据域
    BiTreeNode  *leftChild,  *rightChild,* parent;    //左右子树亲树指针
    BiTreeNode():leftChild(NULL), rightChild(NULL){}
    ~BiTreeNode() {}
};

class BiTree {
public:
    int size;
    BiTreeNode *root;    //根结点指针
    string sTree;        //建树字符串
    int pos;            //标识建树字符串的当前字符位置
    int deep;
    char datatemp;
    char datatemp1;
    char datatemp2;
    int distanceMax;
    BiTreeNode* CreateTree();
    void deepCount(BiTreeNode  *t,int height);
    void distanceCount(BiTreeNode *t);
    BiTree():root(NULL) {};
    void Create(string vArray);    //建树公有接口,参数是特定的先序遍历字符串
    void levelOrder(BiTreeNode* t){
        queue<BiTreeNode*>tq;
        BiTreeNode* p=t;
        if(p!=NULL)tq.push(p);
        while(!tq.empty()){
            cout<<tq.front()->data;
            if(tq.front()->leftChild)tq.push(tq.front()->leftChild);
            if(tq.front()->rightChild)tq.push(tq.front()->rightChild);
            tq.pop();
        }
    }
};
//二叉树公有接口的实现
void BiTree::Create(string vArray)
{    pos=0;
    size=0;
    deep=0;
    distanceMax=0;
    sTree.assign(vArray);    //把参数保存到内部字符串
    root = CreateTree();    //建树成功后root指向根结点
}

BiTreeNode * BiTree::CreateTree(){
    BiTreeNode* t;
    char ch=sTree[pos++];
    if(ch=='#')t=NULL;
    else{
        size++;
        t=new BiTreeNode();
        t->data=ch;
        t->leftChild=CreateTree();
        if(t->leftChild)t->leftChild->parent=t;
        t->rightChild=CreateTree();
        if(t->rightChild)t->rightChild->parent=t;
    }
    return t;
}

 

标签:遍历,随想录,BiTreeNode,tq,二叉树,NULL,root
From: https://www.cnblogs.com/zhishikele/p/17040790.html

相关文章

  • 代码随想录算法训练营第14天
    今日开始二叉树部分,先看了理论基础,刷题3道:递归遍历,迭代遍历, 统一迭代。● 递归遍历题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89......
  • 力扣102 二叉树的层序遍历(广度优先搜索)
    题目:给你二叉树的根节点root,返回其节点值的层序遍历。(即逐层地,从左到右访问所有节点)。示例:输入:root=[3,9,20,null,null,15,7]输出:[[3],[9,20],[15,7]]思......
  • 力扣226 翻转二叉树
    题目:给你一棵二叉树的根节点root,翻转这棵二叉树,并返回其根节点。示例:输入:root=[4,2,7,1,3,6,9]输出:[4,7,2,9,6,3,1] ......
  • 代码随想录算法训练营第八天LeetCode28, 459
    代码随想录算法训练营第八天|LeetCode28,459Leetcode28找出字符串中第一个匹配项的下标题目链接:https://leetcode.cn/problems/find-the-index-of-the-first-occurrence......
  • 判断是不是平衡二叉树
    题目描述输入一棵节点数为n二叉树,判断该二叉树是否是平衡二叉树。在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树平衡二叉树(BalancedBinaryTree),具有以......
  • 判断是不是完全二叉树
    题目要求给定一个二叉树,确定他是否是一个完全二叉树。完全二叉树的定义:若二叉树的深度为h,除第h层外,其它各层的结点数都达到最大个数,第h层所有的叶子结点都连续集中......
  • 代码随想录算法训练营第13天
    今日刷题2道:239.滑动窗口最大值,347.前K个高频元素。● 239.滑动窗口最大值题目链接/文章讲解/视频讲解:https://programmercarl.com/0239.%E6%BB%91%E5%8A%A8%......
  • 二叉树由前序遍历和中序遍历求后序遍历
    题目:二叉树遍历问题描述给定一棵二叉树的先序遍历和中序遍历序列,求其后序遍历序列。输入格式输入数据有两行,为两个字符串,其长度n均小于等于26。第一行为先序遍历序......
  • 代码随想录算法训练营第11天 | 20. 有效的括号 1047. 删除字符串中的所有相邻重复项 1
    20.有效的括号文章:代码随想录(programmercarl.com)视频:栈的拿手好戏!|LeetCode:20.有效的括号_哔哩哔哩_bilibili思路:先来分析一下这里有三种不匹配的情况,第一种......
  • 二叉树递归模板总结
    101.对称二叉树boolisQ(TreeNode*root1,TreeNode*root2){if(root1==nullptr&&root2==nullptr){returntrue;}elseif(roo......