二叉树

查找二叉树中最大的搜索二叉树拓扑结构

//暴力解法
//1. 问题描述:存在的最大的二叉树拓扑结构,假定数据不会重复
    public boolean check(Node node,int value){
        if (node==null)
            return false;
        if (node.val==value)
            return true;
        else if (node.val>value)    //往左搜索
            return check(node.left,value);
        else    //往右搜索
            return check(node.right,value);
    }
//2.先序遍历搜集最大拓扑结构
public int search(Node head,Node node){
        int i=0;
        if (node==null)
            return 0;
        if (check(head,node.val))
            i++;
        int left=search(head,node.left);   //左子树
        int right=search(head,node.right); //右子树
        return left+right+i;
    }
//3.*搜索全部结点,最大的拓扑结构的首结点
    int max;
    Node node1;
    public void query(Node node){
        if (node==null)
            return;
        int i=search(node,node);
        if (max rSize ? lBST : rBST;
    }

96.不同的二叉搜索树
给定n,求可以构成多少种二叉搜索树

dp(n)=dp(0)dp(n-1)+dp(1)dp(n-2)+dp(2)dp(n-3)+…+dp(n-1)dp(0)

95. 不同的二叉搜索树 II
给定n,返回可以构成的搜索二叉树; 这里使用了函数指针
102.二叉树的层次遍历
queue,需要按层打印则需要两外维护两个TreeNode,last和nlast

 vector> levelOrder(TreeNode* root) {
        if(root == NULL) return {};
        queue q;
        vector > result;
        vector tmp;
        q.push(root);
        TreeNode* last=root;
        TreeNode* nlast=root;
        while(!q.empty()){
            TreeNode *t = q.front();
            q.pop();
            tmp.push_back(t->val);
            if (t->left != NULL){
                q.push(t->left);
                nlast=t->left;           
            }
            if (t->right != NULL){
                q.push(t->right);
                nlast=t->right;
            }
            if (last == t){
                result.push_back(tmp);
                tmp.clear();
                last=nlast;
            }
        }
        return result;
    }

107. 二叉树的层次遍历 II
反向打印,只需要把result.push_back()改成result.insert()即可
103. 二叉树的锯齿形层次遍历
queue,需要维护一个change,表示是否需要反转

 vector> zigzagLevelOrder(TreeNode* root) {
        if (!root) return {};
        vector> result;
        queue q;
        q.push(root);
        bool change = false;
        while(!q.empty()){
            int n = q.size();
            vector res;
            while(n--){
                TreeNode* tmp=q.front();
                q.pop();  
                res.push_back(tmp->val);
                if(tmp->left != NULL) q.push(tmp->left);
                if(tmp->right != NULL) q.push(tmp->right);
            }
            if(change){
                result.push_back(vector(res.rbegin(), res.rend()));
            }else{
                result.push_back(res);
            }
            change = !change;
        }
        return result;
    }

二叉树后序非递归

void postorderTraversalNew(TreeNode *root, vector &path)
{
    stack< pair > 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)
        {
            path.push_back(root->val);
        }
        else
        {
            s.push(make_pair(root, true));
            s.push(make_pair(root->right, false));
            s.push(make_pair(root->left, false));
        }
    }
}

树的子结构

public boolean HasSubtree(TreeNode root1, TreeNode root2) {   
        boolean result = false;
        if(root1 != null && root2 != null) {   
            if(root1.val == root2.val) {
                result = judge(root1, root2);
            }
            if(!result) {
                result = HasSubtree(root1.left, root2);
            }
            if(!result) {
                result = HasSubtree(root1.right, root2);
            }
        }
        return result;
    }
    private boolean judge(TreeNode root1, TreeNode root2) {      
        if(root2 == null)
            return true;
        if(root1 == null)
            return false;
        if(root1.val != root2.val)
            return false;
        return judge(root1.left, root2.left) && judge(root1.right, root2.right);
    }

前序中序构造二叉树

TreeNode *buildTree(vector &pre, vector &in) {
        if (pre.size() == 0) return NULL;
        return build(pre, in , 0, pre.size()-1, 0, in.size()-1);
    }
    TreeNode *build(vector &pre,vector &in,int startpre,int endpre,int startin,int endin) {
        if (startpre > endpre || startin > endin) return NULL;
        TreeNode *root = new TreeNode(pre[startpre]);
        int divide = 0;
        while (divide <= endin && in[divide] != root->val) divide++;
        int offset = divide - startin - 1;
        root->left = build(pre, in, startpre + 1, startpre + 1 + offset, startin, startin + offset);
        root->right = build(pre, in, startpre + offset + 2, endpre, divide + 1,endin);
        return root;
    }

二叉搜索树转成双向链表

image.png
 TreeNode* convert(TreeNode* root) {
        if (!root) return root;
        stack st;
        while (root){
            st.push(root);
            root = root->left;
        }
        TreeNode* ans = st.top();
        TreeNode* last = NULL;
        while (!st.empty()){
            TreeNode* tmp = st.top();
            st.pop();
            if (!last) last = tmp;
            else {
                last->right = tmp;
                tmp->left = last;
                last = tmp;
            }
            tmp = tmp->right;
            while (tmp){
                st.push(tmp);
                tmp = tmp->left;
            }
        }
        return ans;
    }


void Convert(BiNode *root, BiNode *& last)
{
    if(root == NULL)
        return;
    Convert(root->left, last);
    root->left = last;
    if(last != NULL)
        last->right = root;
    last = root;
    Convert(root->right, last);
}
 
BiNode * Convert2BinLink( BiNode *root )
{
    if(root == NULL)    
        return NULL;
    BiNode * last = NULL;
    //二叉排序树转换成排序双向链表
    Convert(root, last);
    //取得双向链表的头指针
    while(root->left != NULL)
        root = root->left;
    return root;
}

二叉搜索树的插入

 TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL) {
            return new TreeNode(val);
        }
        if(val < root->val) {
            root->left = insertIntoBST(root->left, val);
        } else {
            root->right = insertIntoBST(root->right, val);
        }
        return root;
    }

你可能感兴趣的:(二叉树)