zoukankan      html  css  js  c++  java
  • leetcode99 Recover Binary Search Tree

      1 """
      2 Two elements of a binary search tree (BST) are swapped by mistake.
      3 Recover the tree without changing its structure.
      4 Example 1:
      5 Input: [1,3,null,null,2]
      6 
      7    1
      8   /
      9  3
     10   
     11    2
     12 Output: [3,1,null,null,2]
     13    3
     14   /
     15  1
     16   
     17    2
     18 Example 2:
     19 Input: [3,1,4,null,null,2]
     20   3
     21  / 
     22 1   4
     23    /
     24   2
     25 Output: [2,1,4,null,null,3]
     26   2
     27  / 
     28 1   4
     29    /
     30   3
     31 """
     32 """
     33 自己AC了一种笨方法:
     34 首先对二叉树中序遍历得到inorder
     35 再对inorder排序得到s_inorder
     36 比较两个列表的差异,如果不相等,将两个不相等的值保存到x,y
     37 第二次遍历二叉树,将值为x的设为'a',值为y的设为x。(这里不知道怎么交换值)
     38 第三次遍历二叉树,将值为'a'的设为y
     39 """
     40 class TreeNode:
     41     def __init__(self, x):
     42         self.val = x
     43         self.left = None
     44         self.right = None
     45 
     46 class Solution1:
     47     def recoverTree(self, root):
     48         """
     49         Do not return anything, modify root in-place instead.
     50         """
     51         stack = []
     52         cur = root
     53         inorder = []
     54         while cur or stack:
     55             if cur:
     56                 stack.append(cur)
     57                 cur = cur.left
     58             else:
     59                 cur = stack.pop()
     60                 inorder.append(cur.val)
     61                 cur = cur.right
     62         s_inorder = sorted(inorder)
     63         for i in range(len(s_inorder)):
     64             if s_inorder[i] != inorder[i]:
     65                 x = s_inorder[i]
     66                 y = inorder[i]
     67                 break
     68         queue = []
     69         queue.append(root)
     70         while queue:
     71             node = queue.pop(0)
     72             if node.val == x:
     73                 node.val = 'a'
     74             if node.val == y:
     75                 node.val = x
     76             if node.left:
     77                 queue.append(node.left)
     78             if node.right:
     79                 queue.append(node.right)
     80         newqueue = []
     81         newqueue.append(root)
     82         while newqueue:
     83             _node = newqueue.pop(0)
     84             if _node.val == 'a':
     85                 _node.val = y
     86             if _node.left:
     87                 newqueue.append(_node.left)
     88             if _node.right:
     89                 newqueue.append(_node.right)
     90 """
     91 解法二:可以再解法一得到有序的s_inorder后
     92 将s_inorder覆盖到整个二叉树
     93 """
     94 class Solution2:
     95     def recoverTree(self, root):
     96         """
     97         Do not return anything, modify root in-place instead.
     98         """
     99         stack = []
    100         cur = root
    101         inorder = []
    102         while cur or stack:
    103             if cur:
    104                 stack.append(cur)
    105                 cur = cur.left
    106             else:
    107                 cur = stack.pop()
    108                 inorder.append(cur.val)
    109                 cur = cur.right
    110         s_inorder = sorted(inorder)
    111         i = 0
    112         _cur = root
    113         _stack = []
    114         while _cur or _stack:
    115             if _cur:
    116                 _stack.append(_cur)
    117                 _cur = _cur.left
    118             else:
    119                 _cur = _stack.pop()
    120                 _cur.val = s_inorder[i]
    121                 i += 1
    122                 _cur = _cur.right
    123 
    124 if __name__ == '__main__':
    125     root = TreeNode(1)
    126     node1 = TreeNode(3)
    127     node2 = TreeNode(2)
    128     root.left = node1
    129     node1.right = node2
    130     ans = Solution1()
    131     print(ans.recoverTree(root))
  • 相关阅读:
    javascript面向对象程序设计之浅谈2
    Sphinx学习之sphinx的安装篇
    IT人的职业生涯规划
    perconatoolkit系列之系统类工具的使用
    perconatoolkit系列之实用类工具使用
    查询ip归属地的shell脚本
    使用mysqlsla分析Mysql数据库日志
    MYSQL管理之主从同步管理
    MYSQL数据库管理之权限管理
    perconatoolkit系列之复制类工具使用
  • 原文地址:https://www.cnblogs.com/yawenw/p/12408780.html
Copyright © 2011-2022 走看看