Leetcode Tree 知識點總結

2021-08-20 01:12:56 字數 4027 閱讀 3021

思路:先確定root,在遞迴獲取root.left和root.right
# 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):

defgeneratetrees

(self, n):

""" :type n: int

:rtype: list[treenode]

"""arrays = [i for i in range(1,n+1)]

return self.dp(arrays)

defdp(self,arrays):

if len(arrays) == 1:

return [treenode(arrays[0])]

ret =

for k in range(len(arrays)):

num = arrays[k]

l = arrays[0:k]

r = arrays[k+1:]

left = self.dp(l)

right = self.dp(r)

if left == :

for j in range(len(right)):

node = treenode(num)

node.right = right[j]

if right == :

for i in range(len(left)):

node = treenode(num)

node.left = left[i]

for i in range(len(left)):

for j in range(len(right)):

node = treenode(num)

node.left = left[i]

node.right = right[j]

return ret

二叉搜尋樹的性質是中序遍歷結果為有序陣列,利用這個性質先獲取原樹中序遍歷的結果,然後尋找最高波峰和最低低谷,再用dfs來修正原來的樹即可
# definition for a binary tree node.

# class treenode(object):

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

import sys

class

solution

(object):

defrecovertree

(self, root):

""" :type root: treenode

:rtype: void do not return anything, modify root in-place instead.

"""stack = [root]

arrays = [-sys.maxint]

while(stack):

item = stack.pop()

if item.left == none

and item.right == none:

else:

if item.right:

if item.left:

val1,val2 = -sys.maxint,sys.maxint

for i in range(1,len(arrays)-1):

if arrays[i]>arrays[i-1] and arrays[i]>arrays[i+1] and arrays[i]>val1:

val1 = arrays[i]

if arrays[i]1] and arrays[i]1] and arrays[i]def

dfs(root,val1,val2):

if root.val == val1:

root.val = val2

elif root.val == val2:

root.val = val1

if root.left:

dfs(root.left,val1,val2)

if root.right:

dfs(root.right,val1,val2)

dfs(root,val1,val2)

設定最大最小邊界進行遞迴判斷
# definition for a binary tree node.

# class treenode(object):

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

import sys

class

solution

(object):

defisvalidbst

(self, root):

""" :type root: treenode

:rtype: bool

"""min_ = -sys.maxint

max_ = sys.maxint

return self.checkbst(root,min_,max_)

defcheckbst

(self,root,min_,max_):

if root is

none: return

true

if root.val <= min_ or root.val >= max_:

return

false

return self.checkbst(root.left,min_,root.val) and self.checkbst(root.right,root.val,max_)

計算節點的值為該節點的值+所有子孫節點值的和

# 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):

defprunetree

(self, root):

""" :type root: treenode

:rtype: treenode

"""if self.nodevalues(root) == 0: return

none

root.left = self.prunetree(root.left)

root.right = self.prunetree(root.right)

return root

defnodevalues

(self,root):

if root is

none: return

0return root.val+self.nodevalues(root.left)+self.nodevalues(root.right)

TestLink知識點Mantis知識點

testlink知識點 1 testlink系統提供了六種角色 a guest 只有讀的許可權,適合於檢視測試用例和測試需求,以及專案分析的使用者。b testdesigner 可以開展測試用例和測試需求的所有工作。c tester 只能執行測試用例。d senior tester 可以檢視和維護測...

python大一知識點 python知識點複習

放假歸來,這幾天複習了一下好久不用的python,總結了一下知識點。語法基礎tuple與list的異同都由多個元素組成 tuple由 組成,list由組成 tuple不可變,list可變 tuple表示的是一種結構,而list表示的是多個事物的集合 tuple操作比list快 字串用法要點 轉義符和...

mysql常用知識點 mysql 常用知識點。

mysql u root p show databases show tables select from abc order by id limit 0,10 create database bbb exit mysqldump u root p game home backup.sql mysq...