LeetCode/树操作综合

树一般使用递归方式来拆分成子问题

1. 合并二叉树

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if(!root1&&!root2) return nullptr;
        if(root1&&root2) 
        {root1->val+=root2->val;
        root1->left=mergeTrees(root1->left,root2->left);
        root1->right=mergeTrees(root1->right,root2->right);
    }
        else if(root2) return root2;
        return root1;
    }
};

2. 二叉树的直径(后序遍历)

class Solution {
public:
    int maxval=0;
    int diameterOfBinaryTree(TreeNode* root) {
        traceback(root);
        return maxval;
    }
    int traceback(TreeNode* root){
        if(!root) return 0;
        int left = traceback(root->left);
        int right =  traceback(root->right);
        int diameter = left+right;
        if(diameter>maxval) maxval = diameter;
        return max(left,right)+1;
    }
};

3. 层次遍历

4. 二叉树展开为链表

class Solution {
    public:
    void flatten(TreeNode *root) {
        if (!root) return;
        flatten(root->left);//左子树展成顺序链式
        TreeNode *right = root->right;//暂存右子树
        root->right = root->left;//将链式左子树给右节点
        root->left = NULL;//左节点赋NULL
        while (root->right != NULL) 
            root = root->right; //移到链式右子树末端
        flatten(right);//将原来右子树展开
        root->right = right;//拼接到新右子树末端
    }
};

5. 判断平衡二叉树(后序遍历)

class Solution {
public:
    int height(TreeNode* root) {
        if (root == NULL) return 0;//高度为0
        int leftHeight = height(root->left);//递归左节点并得到其高度
        if(leftHeight==-1) return -1;//用于打断右节点的递归
        int rightHeight = height(root->right);//递归右节点并得到其高度
        //不满足高度条件,传递回一个-1标志,同时打断在获得标志的情况打断运算和递归
        if (leftHeight==-1||rightHeight==-1||abs(leftHeight-rightHeight)>1)  return -1;
        else  return max(leftHeight, rightHeight) + 1;
    }
    bool isBalanced(TreeNode* root) {
        return height(root) >= 0;
    }
};

6. 判断二叉排序树(中序遍历)

//递归实现
class Solution {
public:
    long pre = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        if(!root) return true;
        bool left = isValidBST(root->left);
        if(pre>=root->val||!left) return false;
        pre = root->val;
        bool right = isValidBST(root->right);
        return left&right;
    }
};
//栈实现
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> stack;
        long long inorder = (long long)INT_MIN - 1;

        while (!stack.empty() || root != nullptr) {
            while (root != nullptr) {
                stack.push(root);
                root = root -> left;
            }
            root = stack.top();
            stack.pop();
            // 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
            if (root -> val <= inorder) {
                return false;
            }
            inorder = root -> val;
            root = root -> right;
        }
        return true;
    }
};

7. 删除二叉搜索树中的节点

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) 
    {
        //深度优先遍历树,找对应节点,递归重构树
        if (!root)   return NULL;
        if (key > root->val)    root->right = deleteNode(root->right, key);     // 递归右子树
        else if (key < root->val)    root->left = deleteNode(root->left, key);  //递归左子树
        else    //找到要删除节点
        {
            if (! root->left)   return root->right; // 情况1,欲删除节点无左子
            if (! root->right)  return root->left;  // 情况2,欲删除节点无右子
            TreeNode* node = root->right;           // 情况3,欲删除节点左右子都有 
            while (node->left)          // 寻找欲删除节点右子树的最左节点
                node = node->left;
            node->left = root->left;    // 将欲删除节点的左子树成为其右子树的最左节点的左子树
            root = root->right;         // 欲删除节点的右子顶替其位置,节点被删除
        }
        return root;//没有找到 
    }
};

8. 二叉树剪枝

返回移除了所有不包含 1 的子树的原二叉树

class Solution {
public:
    //信息要往上传递
    TreeNode* pruneTree(TreeNode* root) {
        TreeNode* pre = new TreeNode();
        pre ->right = root;
        dfs(pre);
        return pre->right;
    }
    bool dfs(TreeNode* root){
        if(!root) return false;
        bool left = dfs(root->left);
        bool right = dfs(root->right);
        if(left==0) root->left = nullptr;
        if(right==0) root->right = nullptr;
        return left|right|root->val;
    }
};

9. 路径总和

10. 二叉树应用题

posted @ 2022-05-16 08:15  失控D大白兔  阅读(20)  评论(0编辑  收藏  举报