代码拉取完成,页面将自动刷新
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;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。