ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

力扣练习——59 从二叉搜索树到更大和树

2022-08-07 18:01:56  阅读:211  来源: 互联网

标签:node 树到 right TreeNode 力扣 item 59 root left


1.问题描述

给出二叉 搜索 树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等于 node.val 的所有节点的值之和。

提醒一下,二叉搜索树满足下列约束条件:

节点的左子树仅包含键 小于 节点键的节点。

节点的右子树仅包含键 大于 节点键的节点。

左右子树也必须是二叉搜索树。

 

示例:

tree.png

输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]

输出:[30,36,21,36,35,26,15,33,8]

 

可使用以下main函数:

#include <iostream>

#include <queue>

#include <stack>

#include<cstdlib>

#include <cstring>

#include<map>

using namespace std;

struct TreeNode

{

    int val;

    TreeNode *left;

    TreeNode *right;

    TreeNode() : val(0), left(NULL), right(NULL) {}

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

    int n,count=0;

    char item[100];

    cin>>n;

    if (n==0)

        return NULL;

    cin>>item;

    TreeNode* root = new TreeNode(atoi(item));

    count++;

    queue<TreeNode*> nodeQueue;

    nodeQueue.push(root);

    while (count<n)

    {

        TreeNode* node = nodeQueue.front();

        nodeQueue.pop();

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int leftNumber = atoi(item);

            node->left = new TreeNode(leftNumber);

            nodeQueue.push(node->left);

        }

        if (count==n)

            break;

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int rightNumber = atoi(item);

            node->right = new TreeNode(rightNumber);

            nodeQueue.push(node->right);

        }

    }

    return root;

}

void printTree(TreeNode* root) {

    if (root == NULL) {

      return;

    }

    bool isFirst=true;

    queue<TreeNode*> q;

    q.push(root);

    while(!q.empty()) {

        TreeNode* node = q.front();

        q.pop();

        if (node == NULL) {

          continue;

        }

        if (!isFirst)

            cout<<",";

        cout<<node->val;

        isFirst=false;

        q.push(node->left);

        q.push(node->right);

    }

}

int main()

{

    TreeNode* root;

    root=inputTree();

    TreeNode* res=Solution().bstToGst(root);

    printTree(res);

}

 

2.输入说明

首先输入结点的数目n(注意,这里的结点包括题中的null空结点)

然后输入n个结点的数据,需要填充为空的结点,输入null。

3.输出说明

输出节点的信息,以逗号分隔

4.范例

输入

15
4 1 6 0 2 5 7 null null null 3 null null null 8

 输出

30,36,21,36,35,26,15,33,8

5.代码

#include <iostream>

#include <queue>

#include <stack>

#include<cstdlib>

#include <cstring>

#include<map>

using namespace std;

struct TreeNode

{

    int val;

    TreeNode *left;

    TreeNode *right;

    TreeNode() : val(0), left(NULL), right(NULL) {}

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

    int n, count = 0;

    char item[100];

    cin >> n;

    if (n == 0)

        return NULL;

    cin >> item;

    TreeNode* root = new TreeNode(atoi(item));

    count++;

    queue<TreeNode*> nodeQueue;

    nodeQueue.push(root);

    while (count < n)

    {

        TreeNode* node = nodeQueue.front();

        nodeQueue.pop();

        cin >> item;

        count++;

        if (strcmp(item, "null") != 0)

        {

            int leftNumber = atoi(item);

            node->left = new TreeNode(leftNumber);

            nodeQueue.push(node->left);

        }

        if (count == n)

            break;

        cin >> item;

        count++;

        if (strcmp(item, "null") != 0)

        {

            int rightNumber = atoi(item);

            node->right = new TreeNode(rightNumber);

            nodeQueue.push(node->right);

        }

    }

    return root;

}

void printTree(TreeNode* root) {

    if (root == NULL) {

        return;

    }

    bool isFirst = true;

    queue<TreeNode*> q;

    q.push(root);

    while (!q.empty()) {

        TreeNode* node = q.front();

        q.pop();

        if (node == NULL) {

            continue;

        }

        if (!isFirst)

            cout << ",";

        cout << node->val;

        isFirst = false;

        q.push(node->left);

        q.push(node->right);

    }

}
int sum = 0;
TreeNode*  bstToGst(TreeNode *root)
{
    
    //反向中序遍历二叉搜索树
    if (root != NULL)
    {
        bstToGst(root->right);//右子树
        sum += root->val;//进行累加操作
        root->val = sum;
        bstToGst(root->left);//左子树
    }
    return root;
}

int main()

{

    TreeNode* root;

    root = inputTree();

    TreeNode* res = bstToGst(root);

    printTree(res);

}

 

标签:node,树到,right,TreeNode,力扣,item,59,root,left
来源: https://www.cnblogs.com/juillard/p/16559517.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有