leetcode 538把二叉搜索树转换为累加树

简介: leetcode 538把二叉搜索树转换为累加树

二叉搜索树转换为累加树


5ad06b600da64dfe8d530633e0e07623.png

中序递归法

/**
 * 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 sum = 0;
    int pre_sum=0;
    //递归遍历计算总和
    void tarversal(TreeNode* cur)
    {
        if(cur==nullptr) return ;
        tarversal(cur->left);
        sum += cur->val;
        tarversal(cur->right);
    }
    //递归对每一个节点值进行修改
    void add_tree(TreeNode* cur )
    {
        if(cur == nullptr) return ;
        add_tree(cur->left);
        int tmp = cur->val;
        cur->val = sum - pre_sum;
        pre_sum += tmp;
        add_tree(cur->right  );
    }
    TreeNode* convertBST(TreeNode* root) {
        tarversal(root);
        add_tree(root);
        return root;
    }
};

逆中序递归法

c64319e41d1942cdb6ee1c1b18d6a454.png

逆中序遍历二叉搜索树,就是从大到小的输出。

当前点的新值就等于上一个点值加上当前点旧值

遍历的顺序要是右中左

class Solution {
private:
    int pre; // 记录前一个节点的数值
    void traversal(TreeNode* cur) { // 右中左遍历
        if (cur == NULL) return;
        traversal(cur->right);
        cur->val += pre;
        pre = cur->val;
        traversal(cur->left);
    }
public:
    TreeNode* convertBST(TreeNode* root) {
        pre = 0;
        traversal(root);
        return 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 addNum = 0;
    void track_back(TreeNode *cur)
    {
        if(cur == nullptr) return ;
        track_back(cur->right);
        cur->val += addNum;
        addNum = cur->val;
        track_back(cur->left);
        return;
    }
    TreeNode* convertBST(TreeNode* root) {
        track_back(root);
        return root;
    }
};
相关文章
|
3天前
leetcode代码记录(不同的二叉搜索树
leetcode代码记录(不同的二叉搜索树
8 0
|
3天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
3天前
|
算法 API DataX
二叉树(下)+Leetcode每日一题——“数据结构与算法”“对称二叉树”“另一棵树的子树”“二叉树的前中后序遍历”
二叉树(下)+Leetcode每日一题——“数据结构与算法”“对称二叉树”“另一棵树的子树”“二叉树的前中后序遍历”
|
3天前
|
算法 DataX
二叉树(中)+Leetcode每日一题——“数据结构与算法”“剑指Offer55-I. 二叉树的深度”“100.相同的树”“965.单值二叉树”
二叉树(中)+Leetcode每日一题——“数据结构与算法”“剑指Offer55-I. 二叉树的深度”“100.相同的树”“965.单值二叉树”
Leetcode1038. 从二叉搜索树到更大和树(每日一题)
Leetcode1038. 从二叉搜索树到更大和树(每日一题)
|
3天前
|
Java
LeetCode题解-二叉搜索树中第K小的元素-Java
LeetCode题解-二叉搜索树中第K小的元素-Java
13 0
|
3天前
LeetCode 树-简单题 4个典例
LeetCode 树-简单题 4个典例
15 0
|
3天前
|
算法
代码随想录Day34 LeetCode T343整数拆分 T96 不同的二叉搜索树
代码随想录Day34 LeetCode T343整数拆分 T96 不同的二叉搜索树
32 0
LeetCode | 572. 另一棵树的子树
LeetCode | 572. 另一棵树的子树
LeetCode | 100. 相同的树
LeetCode | 100. 相同的树