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


    给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。

    例如:

    输入: 二叉搜索树:
    5
    /
    2 13

    输出: 转换为累加树:
    18
    /
    20 13

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree

     1 public class ConvertBSTtoGreaterTree {
     2     static class TreeNode{
     3         int val;
     4         TreeNode left;
     5         TreeNode right;
     6         TreeNode(int x) {
     7             val = x;
     8         }    
     9     }
    10     
    11     private int sum = 0;
    12     public TreeNode convertBST(TreeNode root) {
    13         if(root == null) {
    14             return null;
    15         }
    16         convertBST(root.right);
    17         int temp = root.val;
    18         root.val += sum;
    19         sum += temp;
    20         convertBST(root.left);
    21         return root;
    22     }
    23 }
    无论有多困难,都坚强的抬头挺胸,人生是一场醒悟,不要昨天,不要明天,只要今天。不一样的你我,不一样的心态,不一样的人生,顺其自然吧
  • 相关阅读:
    Vue—node
    Vue—组件,父传子,子传父
    Vue—实例成员computed和watch
    Vue—条件、循环指令
    Vue初识
    BBS—登录,修改密码,退出登录
    创建ORM模型
    模板介绍
    url映射的时候指定默认参数
    自定义url转换器
  • 原文地址:https://www.cnblogs.com/xiyangchen/p/11135932.html
Copyright © 2020-2023  润新知