1 Star 1 Fork 1

千羽 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Solution.java 2.49 KB
一键复制 编辑 原始数据 按行查看 历史
千羽 提交于 2022-05-06 17:02 . 死磕算法系列
package com.nateshao.leetcode.binary_tree.code21_538_convertBST;
/**
* @date Created by 邵桐杰 on 2022/5/6 15:12
* @微信公众号 千羽的编程时光
* @个人网站 www.nateshao.cn
* @博客 https://nateshao.gitlab.io
* @GitHub https://github.com/nateshao
* @Gitee https://gitee.com/nateshao
* Description:
* 538. 把二叉搜索树转换为累加树
* 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
*
* 提醒一下,二叉搜索树满足下列约束条件:
*
* 节点的左子树仅包含键 小于 节点键的节点。
* 节点的右子树仅包含键 大于 节点键的节点。
* 左右子树也必须是二叉搜索树。
* 注意:本题和 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ 相同
*
* 示例 1:
*
* 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
* 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
* 示例 2:
*
* 输入:root = [0,null,1]
* 输出:[1,null,1]
* 示例 3:
*
* 输入:root = [1,0,2]
* 输出:[3,3,2]
* 示例 4:
*
* 输入:root = [3,2,4,1]
* 输出:[7,9,4,10]
*
*/
public class Solution {
/**
* BST的中序遍历就是从小到大,那么反过来就是从大到小,然后累加就好了.
*
* @param root
* @return
*/
public TreeNode convertBST(TreeNode root) {
traverse(root);
return root;
}
int sum = 0;
private void traverse(TreeNode root) {
if (root == null) return;
traverse(root.right);
sum += root.val;
root.val = sum;
traverse(root.left);
}
/*******************/
int num = 0;
public TreeNode convertBST2(TreeNode root) {
if (root != null) {
//遍历右子树
convertBST(root.right);
//遍历顶点
root.val = root.val + num;
num = root.val;
//遍历左子树
convertBST(root.left);
return root;
}
return null;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
Go
1
https://gitee.com/nateshao/leetcode.git
git@gitee.com:nateshao/leetcode.git
nateshao
leetcode
leetcode
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891