538. Convert BST to Greater Tree
题目描述:
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
Input: The root of a Binary Search Tree like this:5/ \2 13Output: The root of a Greater Tree like this:18/ \20 13
解题思路:
起初的思路时间复杂度不好,借鉴别人的思路,DFS是个更好的方法。
先对右儿子进行处理,对值进行累加,再对父节点进行处理,最后对左儿子进行处理。
代码:
起初的代码:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* convertBST(TreeNode* root) { 13 if (root == NULL) 14 return NULL; 15 root->right = convertBST(root->right); 16 if (root->right != NULL) 17 root->val = root->val + findLeft(root->right); 18 if (root->left != NULL) { 19 TreeNode* tmp = findR(root->left); 20 tmp->val = tmp->val + root->val; 21 } 22 root->left = convertBST(root->left); 23 /*if (root->left != NULL) 24 root->left->val = root->left->val + root->val;*/ 25 return root; 26 } 27 int findLeft(TreeNode* root) { 28 while (root->left != NULL) { 29 root = root->left; 30 } 31 return root->val; 32 } 33 TreeNode* findR(TreeNode* root) { 34 while (root->right != NULL) { 35 root = root->right; 36 } 37 return root; 38 } 39 };
View Code
DFS:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* convertBST(TreeNode* root) { 13 int sum = 0; 14 DFS(root, sum); 15 return root; 16 } 17 void DFS(TreeNode* root, int& sum) { 18 if (!root) 19 return; 20 DFS(root->right, sum); 21 sum += root->val; 22 root->val = sum; 23 DFS(root->left, sum); 24 } 25 };
View Code
转载于:https://www.cnblogs.com/gsz-/p/9564354.html
538. Convert BST to Greater Tree相关推荐
- 538. Convert BST to Greater Tree*
538. Convert BST to Greater Tree* https://leetcode.com/problems/convert-bst-to-greater-tree/ 题目描述 Gi ...
- Leetcode——538. Convert BST to Greater Tree
题目原址 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 Given a Binary Sear ...
- 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 538. Convert BST to Greater Tree 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 原始二叉搜索树: ...
- Leetcode 538 - Convert BST to Greater Tree
题意 给一个BST,改变其节点的值,将其节点的值加上所有比他大的节点的值 思路 首先注意是BST,其中序遍历的节点是递增序列.即假设中序遍历后的结果是a0,a1,a2,....ana_0, a_1, ...
- Golang Leetcode 538. Convert BST to Greater Tree.go
思路 因为给定的树是BST,所以可以知道如果采用后续遍历的方式,第一个找到的节点应该是最大的节点.所以采用递归的方式从最大的节点开始改变树的值 code var sum intfunc convert ...
- LeetCode: Convert BST to Greater Tree
利用好BST的特性,解起来并不难 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int va ...
- 11/100. Convert BST to Greater Tree
给定一个二叉搜索树(左节点值<根节点值<右节点值),输出:左=左+根+右,根=根+右,右=右. 从右子树至左子树进行相加,返回left(相加最大值),"temp"用来记 ...
- BST(Binary Search Tree 二叉查找树模版)
/****************************************** 数据结构: BST(Binary Search Tree),二叉查找树;性质: 若结点的左子树不空,则左子树上所 ...
最新文章
- Error:This Gradle plugin requires Studio 3.0 minimum
- ASP.NET文件的下载
- 互联网公司Java面试总结
- java windows7 环境变量_Windows7环境变量中,系统变量与用户变量的优先级
- 小甲鱼 OllyDbg 教程系列 (十一) : inline patch ( 内嵌补丁 )
- 华米科技将推出首款真无线耳机 支持降噪还能检测心率?
- codevs3732==洛谷 解方程P2312 解方程
- Security+ 学习笔记29 虚拟化
- 【Mac】 自带的播放器quicktimeplayer 如何带声音2倍速播放
- 闲鱼x-sign, x-mini-wua算法签名接口调用
- windows使用DD刻录工具刻录U盘
- 可以将pdf转换成jpg图片格式的方法
- 优盘格式化后如何免费恢复
- 【干货】针对DNN的神经网络中上下文相关处理的连续学习概述
- 服务器修改host的ip,主机IP地址设置
- vb.net 实现图片圆形渐变模糊
- 20190401每周精品之理财
- 食品的英语名称总结-----实用
- 《Visual C++ 2010入门教程》系列二:安装、配置和首次使用VS2010
- 小程序ssl报错java,抖音(字节跳动)小程序遇到request(https)请求报错,SSL证书验证失败...
热门文章
- FPGA 开平方方法
- ios 打印 详细错误日志_关于Xcode不能打印崩溃日志
- java在线编程题_阿里笔试题(2017在线编程题)-- 数串分组 --Java实现
- python中intvar_在Python线程中使用Intvar/DoubleVar是否安全?
- matlab中文文档_Linux下Matlab安装
- java线程stop re_Java 多线程 之 stop停止线程实例
- 教你如何处理Nginx禁止ip加端口访问的问题
- 导致网速变慢的安全隐患
- Linux中rsync备份数据使用实例
- 77底盒和86底盒的区别_86型开关底盒的具体参数