zoukankan      html  css  js  c++  java
  • interview_prepare_binarytree

    1.pathSum

     1 class TreeNode:
     2     def __init__(self,x):
     3         self.val=x
     4         self.left=None
     5         self.right=None
     6 
     7 
     8 class Solution:
     9     def dfs(self,root,target,path):
    10         if(root.left==None and root.right==None and root.val+sum(path)==target):
    11             self.res.append(path+[root.val])
    12             return
    13         
    14         if(root.left):
    15             self.dfs(root.left,target,path+[root.val])
    16         
    17         if(root.right):
    18             self.dfs(root.right,target,path+[root.val])
    19 
    20     def pathSum_dfs(self,root,target):
    21         self.res=[]
    22         if(not root):
    23             return []
    24 
    25         self.dfs(root,target,[])
    26 
    27         return self.res
    28 
    29 
    30 
    31 
    32 root=TreeNode(5)
    33 l11=TreeNode(4)
    34 l12=TreeNode(8)
    35 l21=TreeNode(11)
    36 l23=TreeNode(13)
    37 l24=TreeNode(4)
    38 l31=TreeNode(7)
    39 l32=TreeNode(2)
    40 l35=TreeNode(5)
    41 l36=TreeNode(1)
    42 
    43 root.left=l11
    44 root.right=l12
    45 l11.left=l21
    46 l12.left=l23
    47 l12.right=l24
    48 l21.left=l31
    49 l21.right=l32
    50 l24.left=l35
    51 l24.right=l36
    52 
    53 print(Solution().pathSum_dfs(root,22))
     
    dfs+iteration
     1 self.res=[]
     2         if(not root):
     3             return []
     4 
     5         stack=[(root,target-root.val,[root.val])]
     6 
     7         while(len(stack)!=0):
     8             cur,val,path=stack.pop()
     9 
    10             if(cur.left==None and cur.right==None and val==0):
    11                 self.res.append(path)
    12                 
    13             if(cur.right):
    14                 stack.append((cur.right,val-cur.right.val,path+[cur.right.val]))
    15             if(cur.left):
    16                 stack.append((cur.left,val-cur.left.val,path+[cur.left.val]))
    17         return self.res

    2. common ancestor

    3. binary tree to list

     

     1 # Definition for a binary tree node.
     2 # class TreeNode(object):
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 
     8 class Solution(object):
     9     def flatten(self, root):
    10         if(root==None or (root.left==None and root.right==None)):
    11             return root
    12 
    13         left=self.flatten(root.left)
    14         right=self.flatten(root.right)
    15 
    16         if(not left):
    17             return root
    18         else:
    19             tmp=left
    20             while(tmp.right):
    21                 tmp=tmp.right
    22             
    23             tmp.right=right
    24             root.right=left
    25             root.left=None
    26     
    27         return root

    4. bfs+dfs in binary tree

     1 class TreeNode:
     2     def __init__(self,x):
     3         self.val=x
     4         self.left=None
     5         self.right=None
     6 
     7 class Solution:
     8     def bfs(self,root):
     9         res=[]
    10         if(root==None):
    11             return res
    12         
    13         que=[]
    14         que.append((root,[root.val]))
    15         while(len(que)!=0):
    16             size=len(que)
    17             tmp=[]
    18             for i in range(size):
    19                 cur,path=que.pop(0)                
    20                 tmp.append(cur.val)
    21 
    22                 if(cur.left==None and cur.right==None and sum(path)==22):
    23                     print(path)
    24 
    25                 if(cur.left):
    26                     que.append((cur.left,path+[cur.left.val]))
    27                 
    28                 if(cur.right):
    29                     que.append((cur.right,path+[cur.right.val]))
    30             res.append(tmp)
    31 
    32         return res
    33 
    34 
    35     def dfs(self,root):
    36         res=[]
    37         if(root==None):
    38             return res
    39 
    40         stack=[(root,[root.val])]
    41         while(len(stack)!=0):
    42             cur,path=stack.pop()
    43 
    44             if(cur.left==None and cur.right==None and sum(path)==22):
    45                 res.append(path)
    46             
    47             if(cur.left):
    48                 stack.append((cur.left,path+[cur.left.val]))
    49             
    50             if(cur.right):
    51                 stack.append((cur.right,path+[cur.right.val]))
    52         return res
    53 
    54 
    55 
    56 root=TreeNode(5)
    57 l11=TreeNode(4)
    58 l12=TreeNode(8)
    59 l21=TreeNode(11)
    60 l23=TreeNode(13)
    61 l24=TreeNode(4)
    62 l31=TreeNode(7)
    63 l32=TreeNode(2)
    64 l35=TreeNode(5)
    65 l36=TreeNode(1)
    66 
    67 root.left=l11
    68 root.right=l12
    69 l11.left=l21
    70 l12.left=l23
    71 l12.right=l24
    72 l21.left=l31
    73 l21.right=l32
    74 l24.left=l35
    75 l24.right=l36
    76 
    77 # print(Solution().bfs(root))
    78 print(Solution().dfs(root))

    5. max pathsum in binary tree

     1 # Definition for a binary tree node.
     2 # class TreeNode(object):
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 
     8 class Solution(object):
     9     def helper(self,root):
    10         if(root==None):
    11             return 0
    12         
    13         left=max(self.helper(root.left),0)
    14         right=max(self.helper(root.right),0)
    15 
    16         self.res=max(self.res,left+root.val+right)
    17 
    18         return max(left,right)+root.val
    19 
    20     def maxPathSum(self, root):
    21         if(root==None):
    22             return 0
    23         
    24         self.res=float('-inf')
    25         self.helper(root)
    26 
    27         return self.res

     5. dfs + 中序遍历二叉树

     1 # Definition for a binary tree node.
     2 # class TreeNode(object):
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 
     8 class Solution(object):
     9     def inorderTraversal(self, root):
    10         """
    11         :type root: TreeNode
    12         :rtype: List[int]
    13         """
    14         res=[]
    15         if(root==None):
    16             return res
    17         stack=[(root,"go")]
    18 
    19         while(len(stack)!=0):
    20             cur_nd,cur_cmd=stack.pop()
    21 
    22             if(cur_cmd=='visited'):
    23                 res.append(cur_nd.val)
    24             else:
    25                 if(cur_nd.right):
    26                     stack.append((cur_nd.right,'go'))
    27                 stack.append((cur_nd,'visited'))
    28                 if(cur_nd.left):
    29                     stack.append((cur_nd.left,'go'))
    30             
    31         return res

    6. 不同的BST个数

     1 class Solution(object):
     2     def numTrees(self, n):
     3         """
     4         :type n: int
     5         :rtype: int
     6         """
     7         if(n==1):return 1
     8         
     9         ### 初始定义
    10         dp=[0 for i in range(n+1)]
    11         dp[0]=1
    12         dp[1]=1
    13 
    14         for i in range(2,n+1):
    15             for j in range(i):
    16                 dp[i]+=dp[j]*dp[i-1-j]
    17         
    18         return dp[n]

    7.根据preOrder和inOrder恢复出二叉树

    105. Construct Binary Tree from Preorder and Inorder Traversal

     1 # Definition for a binary tree node.
     2 # class TreeNode(object):
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 
     8 class Solution(object):
     9     def buildTree(self, preorder, inorder):
    10         """
    11         :type preorder: List[int]
    12         :type inorder: List[int]
    13         :rtype: TreeNode
    14         """
    15         if(len(preorder)==0):return None
    16 
    17         root_val=preorder[0]
    18         root=TreeNode(root_val)
    19 
    20         j=0
    21         for j in range(len(inorder)):
    22             if(inorder[j]==root_val):
    23                 break
    24         
    25         root.left=self.buildTree(preorder[1:j+1],inorder[:j])
    26         root.right=self.buildTree(preorder[j+1:],inorder[j+1:])
    27 
    28         return root
  • 相关阅读:
    SDOI2019游记
    noi.ac309 Mas的童年
    51nod1237 最大公约数之和
    loj6074 子序列
    noi.ac89A 电梯
    51nod97B 异或约束和
    bzoj4490 随机数生成器Ⅱ加强版
    CF55D Beautiful numbers
    CF24D Broken robot
    CF226D The table
  • 原文地址:https://www.cnblogs.com/zijidan/p/12500314.html
Copyright © 2011-2022 走看看