zoukankan      html  css  js  c++  java
  • 897. Increasing Order Search Tree

    1. Question:

    897. Increasing Order Search Tree

    url: https://leetcode.com/problems/increasing-order-search-tree/description/

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.

    Example 1:
    Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]
    
           5
          / 
        3    6
       /     
      2   4    8
     /        /  
    1        7   9
    
    Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
    
     1
      
       2
        
         3
          
           4
            
             5
              
               6
                
                 7
                  
                   8
                    
                     9  

    Note:

    1. The number of nodes in the given tree will be between 1 and 100.
    2. Each node will have a unique integer value from 0 to 1000.

    2. Solution:

    # 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 inOrder(self, root, path):
            if root is None:
                return
            self.inOrder(root.left, path)
            path.append(root)
            self.inOrder(root.right, path)
    
        def increasingBST(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            path_node = []
            self.inOrder(root, path_node)
    
            if len(path_node) <= 0:
                return None
    
            re_root = path_node[0]
            tail_node = re_root
    
            for node in path_node[1:]:
                tail_node.left = None
                tail_node.right = node
                tail_node = node
    
            tail_node.left = None
            tail_node.right = None
    
            return re_root
     
  • 相关阅读:
    【bzoj2115】[Wc2011] Xor
    【bzoj2460】[BeiJing2011]元素
    P2300 合并神犇 DP
    P1041 传染病控制 深搜
    P1038 神经网络 图论
    树状数组模板
    送外卖 状压DP
    士兵守卫(同P2016 战略游戏) 树形DP
    P1171 售货员的难题 喻队状压 DP
    P2062 分队问题 DP
  • 原文地址:https://www.cnblogs.com/ordili/p/9976077.html
Copyright © 2011-2022 走看看