In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how the LeetCode binary search tree into a cumulative tree, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
1. Brief introduction of the problem.
The root node of the binary search tree is given, and the node values of the tree are different. Please convert it into a cumulative tree (Greater Sum Tree) so that the new value of node of each node is equal to the sum of values greater than or equal to node.val in the original tree. As a reminder, the binary search tree satisfies the following constraints: the left subtree of a node contains only nodes whose keys are less than the node keys. The right subtree of a node contains only nodes whose keys are greater than the node keys. The left and right subtrees must also be binary search trees.
2, example
Input: [4 recollection 1, 6, 6, 0, 6, 0, 5, and 7] output: [30, 36, 21, 26, 35, 15, 15, 15, 15, 15, 15, and 5] example:
Input: root = [0flore nullpen1] output: [1recorder nullre1] example 3:
Input: root = [1j0jue 2] output: [3Jing 3jue 2] example 4:
Input: root = [3, 2, 4, 4, 1] output: [7, 9, 4, 10]
Tip:
The number of nodes in the tree is between 1 and 100. The value of each node is between 0 and 100. All values in the tree are different from each other. The given tree is a binary search tree.
3, the train of thought of solving the problem
Write the most basic idea to solve the problem, first count the node data of the binary tree, and then calculate it according to the known conditions of the topic, and then assign the data.
4, problem solving procedure
Import java.util.ArrayList;import java.util.List
Public class ConvertBSTTest {static List list = new ArrayList ()
Public static void main (String [] args) {TreeNode T1 = new TreeNode (4); TreeNode T2 = new TreeNode (1); TreeNode T3 = new TreeNode (6); TreeNode T4 = new TreeNode (0); TreeNode T5 = new TreeNode (2); TreeNode T6 = new TreeNode (5); TreeNode T7 = new TreeNode (7); TreeNode T8 = new TreeNode (3); TreeNode T9 = new TreeNode (8) T1.left = T2; t1.right = T3; t2.left = T4; t2.right = T5; t3.left = T6; t3.right = T7; t5.right = T8; t7.right = T9; TreeNode treeNode = convertBST (T1); System.out.println ("treeNode =" + treeNode)
}
Public static TreeNode convertBST (TreeNode root) {if (root = = null) {return null;} if (list.size () = = 0) {dfs (root);} Integer compute = compute (root.val); root.val = compute; if (root.left! = null) {convertBST (root.left) } if (root.right! = null) {convertBST (root.right);}
Return root
}
Private static Integer compute (Integer val) {int sum = 0; for (int num: list) {if (num > val) {sum + = num;}} sum + = val; return sum;}
Private static void dfs (TreeNode root) {if (root = = null) {return;} if (root.left! = null) {dfs (root.left);} list.add (root.val); if (root.right! = null) {dfs (root.right);}
5. Picture version of the problem solving program.
six
Thank you for reading this article carefully. I hope the article "how to convert a binary search tree into a cumulative tree by LeetCode" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.