538. Convert BST to Greater Tree

本文介绍如何将二叉搜索树(BST)转换为大于等于树,通过三种方法实现:递归逆中序遍历、迭代使用栈以及Morris遍历。每种方法均有详细代码示例。

题目描述:

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.

 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
思路一:使用递归

用逆中序遍历“右根左”,降序遍历BST中的结点。


class Solution {
    int sum = 0;
    public TreeNode convertBST(TreeNode root) {
        convert(root);
        return root;
    }
    public void convert(TreeNode node)
    {
        if (node == null)
            return;
        convert(node.right);
        node.val += sum;
        sum = node.val;
        convert(node.left);
    }
}
若不使用全局变量

class Solution {
    public TreeNode convertBST(TreeNode root) {
        dfs(root, 0);
        return root;
    }
    public int dfs(TreeNode node, int val)
    {
        if (node == null)
            return val;
        int right = dfs(node.right, val);
        node.val += right;
        int left = dfs(node.left, node.val);
        return left;
    }
}
思路二:使用迭代,栈

class Solution {
    public TreeNode convertBST(TreeNode root) {
        if (root == null)
            return null;
        int sum = 0;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode currNode = root;
        while (currNode != null || !stack.isEmpty())
        {
            while (currNode != null)
            {
                stack.push(currNode);
                currNode = currNode.right;
            }
            currNode = stack.pop();
            currNode.val += sum;
            sum = currNode.val;
            currNode = currNode.left;
        }
        return root;
    }
}
思路三:

Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)。

class Solution {
    public TreeNode convertBST(TreeNode root) {
        if (root == null)
            return null;
        int sum = 0;
        TreeNode curr = root;
        while (curr != null)
        {
            if (curr.right == null)
            {
                curr.val += sum;
                sum = curr.val;
                curr = curr.left;
            }
            else
            {
                TreeNode pre = curr.right;
                while (pre.left != null && pre.left != curr)
                    pre = pre.left;
                if (pre.left == null)
                {
                    pre.left = curr;
                    curr = curr.right;
                }
                else
                {
                    pre.left = null;
                    curr.val += sum;
                    sum = curr.val;
                    curr = curr.left;
                }
            }
        }
        return root;
    }
}








评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值