首页 > 其他分享 >LeetCode - Medium - 538

LeetCode - Medium - 538

时间:2024-09-22 13:50:56浏览次数:3  
标签:node Medium TreeNode sum BinaryTree null root LeetCode 538

  • The right subtree of a node contains only nodes with keys greater than the node’s key.

  • Both the left and right subtrees must also be binary search trees.

Note: This question is the same as 1038: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/

Example 1:

Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]

Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Example 2:

Input: root = [0,null,1]

Output: [1,null,1]

Example 3:

Input: root = [1,0,2]

Output: [3,3,2]

Example 4:

Input: root = [3,2,4,1]

Output: [7,9,4,10]

Constraints:

  • The number of nodes in the tree is in the range [ 0 , 1 0 4 ] [0, 10^4] [0,104].

  • ? 1 0 4 < = N o d e . v a l < = 1 0 4 -10^4 <= Node.val <= 10^4 ?104<=Node.val<=104

  • All the values in the tree are unique.

  • root is guaranteed to be a valid binary search tree.

Analysis


方法一:中序遍历模式的递归版

方法二:中序遍历模式的迭代版

Submission


import java.util.LinkedList;

import com.lun.util.BinaryTree.TreeNode;

public class ConvertBSTToGreaterTree {

//方法一:中序遍历模式的递归版

public TreeNode convertBST(TreeNode root) {

dfs(root, new int[] {0});

return root;

}

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎徽关注公zhong号:编程进阶路 加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

private void dfs(TreeNode node, int[] sum) {

if(node == null)

return;

dfs(node.right, sum);

sum[0] = node.val += sum[0];

dfs(node.left, sum);

}

//方法二:中序遍历模式的迭代版

public TreeNode convertBST2(TreeNode root) {

TreeNode p = root;

LinkedList<TreeNode> stack = new LinkedList<>();

int sum = 0;

while(!stack.isEmpty() || p != null) {

if(p != null) {

stack.push(p);

p = p.right;

}else {

TreeNode node = stack.pop();

sum = node.val += sum;

p = node.left;

}

}

return root;

}

}

Test


import static org.junit.Assert.*;

import org.junit.Test;

import com.lun.util.BinaryTree;

import com.lun.util.BinaryTree.TreeNode;

public class ConvertBSTToGreaterTreeTest {

@Test

public void test() {

ConvertBSTToGreaterTree obj = new ConvertBSTToGreaterTree();

TreeNode root1 = BinaryTree.integers2BinaryTree(4,1,6,0,2,5,7,null,null,null,3,null,null,null,8);

TreeNode expected1 = BinaryTree.integers2BinaryTree(30,36,21,36,35,26,15,null,null,null,33,null,null,null,8);

assertTrue(BinaryTree.equals(obj.convertBST(root1), expected1));

TreeNode root2 = BinaryTree.integers2BinaryTree(0,null,1);

标签:node,Medium,TreeNode,sum,BinaryTree,null,root,LeetCode,538
From: https://blog.51cto.com/u_17015020/12080296

相关文章

  • Leetcode 1041. 困于环中的机器人
    1.题目基本信息1.1.题目描述在无限的平面上,机器人最初位于(0,0)处,面朝北方。注意:北方向是y轴的正方向。南方向是y轴的负方向。东方向是x轴的正方向。西方向是x轴的负方向。机器人可以接受下列三条指令之一:“G”:直走1个单位“L”:左转90度“R”......
  • Leetcode 2464. 有效分割中的最少子数组数目
    1.题目基本信息1.1.题目描述给定一个整数数组nums。如果要将整数数组nums拆分为子数组后是有效的,则必须满足:每个子数组的第一个和最后一个元素的最大公约数大于1,且nums的每个元素只属于一个子数组。返回nums的有效子数组拆分中的最少子数组数目。如果不能进......
  • 【LeetCode Hot 100】15. 三数之和
    题目描述回忆一下之前做过的两数之和,用的是哈希表存储已经遍历过的元素。但是本题要求返回值中不能有重复元素,因此需要去重,强行用哈希表的话,去重操作会很复杂。我们可以通过哪些方法来保证返回的数组中不包含重复的三元组?先将整个数组进行排序,可以保证答案数组中有\((a,b,c)\)......
  • leetcode 算法题目学习笔记 - 序号1
    1.两数之和https://leetcode.cn/problems/two-sum/简要说明:1.给定一个数组和一个数字2.要求找到数组中某两个元素,使得他们相加等于所给数字(将所给数字拆为数组中的某两个个元素)3.以数组形式返回两个下标否则返回空指针返回的下标没有顺序要求假设有唯一解,即只能在数组中......
  • Leetcode 406. 根据身高重建队列
    1.题目基本信息1.1.题目描述假设有打乱顺序的一群人站成一个队列,数组people表示队列中一些人的属性(不一定按顺序)。每个people[i]=[h_i,k_i]表示第i个人的身高为h_i,前面正好有k_i个身高大于或等于h_i的人。请你重新构造并返回输入数组people所表示的队列。返......
  • Leetcode 378. 有序矩阵中第 K 小的元素
    1.题目基本信息1.1.题目描述给你一个nxn矩阵matrix,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。请注意,它是排序后的第k小元素,而不是第k个不同的元素。你必须找到一个内存复杂度优于O(n^2)的解决方案。1.2.题目地址https://leetcode.cn/problem......
  • (LeetCode 热题 100) 199. 二叉树的右视图(递归、深度优先搜索dfs)
    199.二叉树的右视图思路:递归每次都优先右边子树,然后才是左子树。/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode():val(0),left(nullptr),right(nullptr){}......
  • CF538H Summer Dichotomy 题解
    自己做的\^w^/。对于\(m\)个限制,我们得到了一个图,若不是二分图则无解,否则对于每个连通块有\([l_1,r_1],[l_2,r_2]\)的限制,表示对于两组的人数限制(注意此处\(1,2\)并不代表组\(1\),\(2\))。不妨令\(n_1\gen_2,(r_1>r_2\operatorname{or}r_1==r_2\operato......
  • [leetcode刷题]面试经典150题之3删除有序数组中的重复项(简单)
    题目 删除有序数组中的重复项给你一个 非严格递增排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。考虑 nums 的唯一元素的数量为 k ,你......
  • LeetCode 876
    题目:LeetCode876解法一:快慢指针注意:while循环条件,以链表(1,2,3,4,null)为例:当条件为fast!=null&&fast.next!=null时,若链表元素为偶数个,则返回中间的后一个节点(3)当条件为fast.next!=null&&fast.next.next!=null时,若链表元素为偶数个,则返回中间的前一个节......