leetcode 538. Convert BST to Greater Tree

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

 

解法1:

tree遍历顺序,right->root->left,这样tree 节点按照从大到小顺序遍历。

为了不改变原来的tree,先copy下:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        """
use right->root->left order traverse
when for this node, node.val += last_traver_node.val
        """ 
        if not root: return None
        
        def copy_node(root):
            if not root: return None
            r = TreeNode(root.val)
            r.left = copy_node(root.left)
            r.right = copy_node(root.right)
            return r
            
        new_root = copy_node(root)
        
        q = []
        node = new_root
        last_node = None
        while node:
            q.append(node)            
            node = node.right         
        while q:
            n = q.pop() # right->root->left
            if last_node:                
                n.val += last_node.val
            last_node = n
            node = n.left
            while node:
                q.append(node)
                node = node.right
        return new_root                        

如果是允许修改原来的tree则:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        """
use right->root->left order traverse
when for this node, node.val += last_traver_node.val
        """ 
        if not root: return None                
        q = []
        node = root
        last_node = None
        while node:
            q.append(node)            
            node = node.right         
        while q:
            n = q.pop() # right->root->left
            if last_node:                
                n.val += last_node.val
            last_node = n
            node = n.left
            while node:
                q.append(node)
                node = node.right
        return root                        

解法3,DFS,使用python3 利用闭包搞定:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        """
use right->root->left order traverse
when for this node, node.val += last_traver_node.val
        """ 
        last_node = None
        def dfs(node):
            if not node: return None
            dfs(node.right)
            nonlocal last_node
            node.val += (last_node and last_node.val or 0)
            last_node = node
            dfs(node.left)
            return node
        
        return dfs(root)                         

使用一个闭包变量跟踪迭代的每一个node!

 最后一种解法是Morris Traversal

    public TreeNode convertBST(TreeNode root) {
        TreeNode cur= root;
        int sum = 0;
        while (cur != null) {
            if (cur.right == null) {
                int tmp = cur.val;
                cur.val += sum;
                sum += tmp;
                cur = cur.left;
            } else {
                TreeNode prev = cur.right;
                while (prev.left != null && prev.left != cur)
                    prev = prev.left;
                if (prev.left == null) {
                    prev.left = cur;
                    cur = cur.right;
                } else {
                    prev.left = null;
                    int tmp = cur.val;
                    cur.val += sum;
                    sum += tmp;
                    cur = cur.left;
                }
            }
        }
        return root;
    }


其他解法,没有太懂。。。
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        def dfs(node, sum_val):
            if not node: return sum_val
            val = dfs(node.right, sum_val)
            node.val += val
            return dfs(node.left, node.val)

        dfs(root, 0)
        return root
             
            

 

 
posted @ 2018-03-24 22:06  bonelee  阅读(201)  评论(1编辑  收藏  举报