分类:二叉树

分类:二叉树

@二叉树前序遍历(递归)

void preOrder1(BinTree *root)     //递归前序遍历 
{
    if(root!=NULL)
    {
        cout<<root->data<<" ";
        preOrder1(root->lchild);
        preOrder1(root->rchild);
    }
}

@二叉树前序遍历(非递归)

void preOrder2(BinTree *root)     //非递归前序遍历 
{
    stack<BinTree*> s;
    BinTree *p=root;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            s.push(p);
            p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            s.pop();
            p=p->rchild;
        }
    }
}

@二叉树中序遍历(递归)

void inOrder1(BinTree *root)      //递归中序遍历
{
    if(root!=NULL)
    {
        inOrder1(root->lchild);
        cout<<root->data<<" ";
        inOrder1(root->rchild);
    }
}

@二叉树中序遍历(非递归)

void inOrder2(BinTree *root)      //非递归中序遍历
{
    stack<BinTree*> s;
    BinTree *p=root;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)
        {
            s.push(p);
            p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            cout<<p->data<<" ";
            s.pop();
            p=p->rchild;
        }
    }    
}

@二叉树后序遍历(递归)

void postOrder1(BinTree *root)    //递归后序遍历
{
    if(root!=NULL)
    {
        postOrder1(root->lchild);
        postOrder1(root->rchild);
        cout<<root->data<<" ";
    }    
}

@二叉树后序遍历(非递归)

void postOrder2(BinTree *root)    //非递归后序遍历
{
    stack<BTNode*> s;
    BinTree *p=root;
    BTNode *temp;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)              //沿左子树一直往下搜索,直至出现没有左子树的结点 
        {
            BTNode *btn=(BTNode *)malloc(sizeof(BTNode));
            btn->btnode=p;
            btn->isFirst=true;
            s.push(btn);
            p=p->lchild;
        }
        if(!s.empty())
        {
            temp=s.top();
            s.pop();
            if(temp->isFirst==true)     //表示是第一次出现在栈顶 
             {
                temp->isFirst=false;
                s.push(temp);
                p=temp->btnode->rchild;    
            }
            else                        //第二次出现在栈顶 
             {
                cout<<temp->btnode->data<<" ";
                p=NULL;
            }
        }
    }    
}

@二叉树层序遍历

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if(root == NULL) 
            return res;
        vector<int> cur;
        std::vector<TreeNode*> v1, v2;
        v1.push_back(root);
        while(!v1.empty()) {
            for(auto x : v1) {
                cur.push_back(x->val);
                if(x->left) v2.push_back(x->left);
                if(x->right) v2.push_back(x->right);
            }
            res.push_back(cur);
            cur.clear();
            v2.swap(v1);
            v2.clear();
        }
        return res;
    }
};

@已知前序中序(或前序后续,或中序后序)重建二叉树

TreeNode* rebuild(std::vector<int> vec1, std::vector<int> vec2);

@二叉树镜像

TreeNode* mirror(TreeNode* root);

@二叉树的深度

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        int d = 0;
        if(root != NULL) {
            d++;
            int d1 = maxDepth(root->left);
            int d2 = maxDepth(root->right);
            d += max(d1, d2);
        }
        return d;
    }
};

@二叉树中和为某一数值的路径

std::vector<std::vector<int>> findPath(TreeNode* root, int num);

@中序(或前序,或后序)二叉树的下一个节点

TreeNode* findNext(TreeNode* root)

@判断是否为对称二叉树

/**
 * 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:
    bool isSymmetric(TreeNode* root) {
        return isFunc(root, root);
    }
    bool isFunc(TreeNode* root1, TreeNode* root2) {
        if((root1 == NULL && root2 == NULL)
        || (root1 && root2 && root1->val == root2->val 
        && isFunc(root1->left, root2->right) && isFunc(root1->right, root2->left))) {
            return true;
        } else {
            return false;
        }
    }
};

@判断是否为平衡二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(!root) {
            return true;
        }
        int a = hight(root->left);
        int b = hight(root->right);
        return abs(a - b) <= 1 && isBalanced(root->left) && isBalanced(root->right);
    }

    int hight(TreeNode* root) {
        if(!root) {
            return 0;
        } 
        return 1 + max(hight(root->left), hight(root->right));
    }
};

@判断是否为二叉搜索树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        if(root == NULL) {
            return true;
        } else {
            bool blcal = true;
            bool brcal = true;
            bool blBST = true;
            bool brBST = true;
            if(root->left) {
                int imaxLeft = maxVal(root->left);
                blcal = imaxLeft < root->val;
                blBST = isValidBST(root->left);
            }
            if(root->right) {
                int iminRight = minVal(root->right);
                brcal = iminRight > root->val;
                brBST = isValidBST(root->right);
            }
            return blcal && blBST && brcal && brBST;
        }

        return false;
    }

    int maxVal(TreeNode* root) {
        std::queue<TreeNode*> queNum;
        queNum.push(root);
        int imax = root->val;
        while(!queNum.empty()) {
            TreeNode* q = queNum.front();
            queNum.pop();
            imax = max(imax, q->val);
            if(q->left) {
                queNum.push(q->left);
            }
            if(q->right) {
                queNum.push(q->right);
            }
        }
        return imax;
    }

    int minVal(TreeNode* root) {
        std::queue<TreeNode*> queNum;
        queNum.push(root);
        int imin = root->val;
        while(!queNum.empty()) {
            TreeNode* q = queNum.front();
            queNum.pop();
            imin = min(imin, q->val);
            if(q->left) {
                queNum.push(q->left);
            }
            if(q->right) {
                queNum.push(q->right);
            }
        }
        return imin;
    }
};

@判断是否相同的树

/**
 * 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:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if((p == NULL && q == NULL)
            || (p && q && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right))) {
            return true;
        }else {
            return false;
        }
    }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值