Convert BST to Greater Tree
2017-05-11 14:03
274 查看
题目地址:https://leetcode.com/problems/convert-bst-to-greater-tree/#/description
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
一个简单的思路:先把原二叉树所有节点的值存在一个列表中(遍历一次树),然后逐个扫描二叉树(再次遍历),并将每个节点与列表中的值作对比,如果比列表中的值小,那么就将列表中的值加在此节点上。
缺点:时间复杂度为O(n2),优点:适用所有二叉树。
题目中告诉我们给定的输入是BST,上述实现我们没有考虑这个条件,如果把这个条件也考虑进去,我们就可以降低我们的时间复杂度:
试想在BST中根节点右子树的值全部大于根节点的值,左子树的值全部小鱼根节点的值,那么很显然,根节点的最终值肯定是右子树所有节点值与根节点值的和。
在考虑最右节点的值肯定是不会变的,因为它的值最大了,没有那个节点比它牛X,所以它的最终值就是它的初始值。
对于一般的左孩子节点,那么它的最终值等于它右子树的值(所有节点值的加和)+父节点转换后的值。
有了以上规律,那么就很容易写出递归的实现方法:
这个实现的时间复杂度是O(n)
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
Input: The root of a Binary Search Tree like this: 5 / \ 2 13 Output: The root of a Greater Tree like this: 18 / \ 20 13
一个简单的思路:先把原二叉树所有节点的值存在一个列表中(遍历一次树),然后逐个扫描二叉树(再次遍历),并将每个节点与列表中的值作对比,如果比列表中的值小,那么就将列表中的值加在此节点上。
缺点:时间复杂度为O(n2),优点:适用所有二叉树。
public class ConvertBSTtoGreaterTree { public static TreeNode convertBST(TreeNode root) { if (root == null) return null; if (root.left == null && root.right == null) return root; ArrayList<Integer> list = new ArrayList<>(); LinkedList<TreeNode> stack = new LinkedList<>(); stack.push(root); while (!stack.isEmpty()) { TreeNode node = stack.pop(); list.add(node.val); if (node.left != null) stack.push(node.left); if (node.right != null) stack.push(node.right); } stack.clear(); stack.push(root); while (!stack.isEmpty()){ TreeNode node = stack.pop(); int t = node.val; for (Integer n : list){ if (t < n) node.val += n; } if (node.left != null) stack.push(node.left); if (node.right != null) stack.push(node.right); } return root; } public static void main(String[] args) { //[5,2,13,1,3,11,14] TreeNode root = new TreeNode(5); root.left = new TreeNode(2); root.right = new TreeNode(13); root.left.left = new TreeNode(1); root.left.right = new TreeNode(3); root.right.left = new TreeNode(11); root.right.right = new TreeNode(14); TreeNode nr = _convertBST(root); return; } }
题目中告诉我们给定的输入是BST,上述实现我们没有考虑这个条件,如果把这个条件也考虑进去,我们就可以降低我们的时间复杂度:
试想在BST中根节点右子树的值全部大于根节点的值,左子树的值全部小鱼根节点的值,那么很显然,根节点的最终值肯定是右子树所有节点值与根节点值的和。
在考虑最右节点的值肯定是不会变的,因为它的值最大了,没有那个节点比它牛X,所以它的最终值就是它的初始值。
对于一般的左孩子节点,那么它的最终值等于它右子树的值(所有节点值的加和)+父节点转换后的值。
有了以上规律,那么就很容易写出递归的实现方法:
public class ConvertBSTtoGreaterTree { int sum = 0; public TreeNode convertBST(TreeNode root) { convert(root); return root; } public void convert(TreeNode cur) { if (cur == null) return; convert(cur.right); cur.val += sum; sum = cur.val; convert(cur.left); } public static void main(String[] args) { //[5,2,13,1,3,11,14] TreeNode root = new TreeNode(5); root.left = new TreeNode(2); root.right = new TreeNode(13); root.left.left = new TreeNode(1); root.left.right = new TreeNode(3); root.right.left = new TreeNode(11); root.right.right = new TreeNode(14); TreeNode nr = _convertBST(root); return; } }
这个实现的时间复杂度是O(n)
相关文章推荐
- 538. Convert BST to Greater Tree
- Leetcode 538:Convert BST to Greater Tree
- [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
- Convert BST to Greater Tree
- LintCode-Convert BST to Greater Tree
- 538. Convert BST to Greater Tree
- LeetCode 538 Convert BST to Greater Tree(二叉树的右中左遍历)
- LeetCode之Convert BST to Greater Tree(Kotlin)
- 538. Convert BST to Greater Tree
- Convert BST to Greater Tree
- Convert BST to Greater Tree
- Convert BST to Greater Tree
- leetcode(538):Convert BST to Greater Tree
- Convert BST to Greater Tree
- LeetCode 538----Convert BST to Greater Tree
- Convert BST to Greater Tree
- LeetCode-538 Convert BST to Greater Tree
- Convert BST to Greater Tree
- lintcode_Convert BST to Greater Tree
- leetcode(538). Convert BST to Greater Tree