單鏈反轉和二叉樹廣度深度遍歷 遞迴非遞迴版

2021-09-28 21:29:15 字數 4071 閱讀 9956

class node:

def __init__(self, x):

self.val = x

self.next = none

l1 = node(3) # 建立鍊錶3->2->1->9->none

l1.next = node(2)

l1.next.next = node(1)

l1.next.next.next = node(9)

1、單鏈反轉

class node:

def __init__(self, data=none, next=none):

self.data = data

self.next = next

def rev(link):

pre = link

cur = link.next

pre.next = none

while cur:

temp = cur.next

cur.next = pre

pre = cur

cur = temp

return pre

if __name__ == '__main__':

link = node(1, node(2, node(3, node(4, node(5, node(6, node(7, node(8, node(9)))))))))

print(link)

root = rev(link)

while root:

print(root.data)

root = root.next

遞迴版本:

def func(head):

if head.next == none:

return head

new_head = func(head.next)

head.next.next = head

head.next = none

return new_head

2、二叉樹廣度遍歷

'''

樹的構建:

39 20

15 7

'''class tree():

'樹的實現'

def __init__(self, data, left=0, right=0):

self.left = left

self.right = right

self.data = data

def __str__(self):

return str(self.data)

# test tree

tree1 = tree(data=15)

tree2 = tree(data=7)

tree3 = tree(20, tree1, tree2)

tree4 = tree(data=9)

base = tree(3, tree4, tree3)

def function(root):

a =

result =

if not root:

return result

while a:

current_root = a.pop(0)

if current_root.left:

if current_root.right:

print(result)

return result

print(function(base))

3、二叉樹深度遍歷(全中後序)

class solution:

def front_search(self,tree_base):

'前序遍歷:根-左-右'

if tree_base == 0:

return

print(tree_base.data)

self.front_search(tree_base.left)

self.front_search(tree_base.right)

def middle_search(self,tree_base):

'中序遍歷:左-根-右'

if tree_base == 0:

return

self.middle_search(tree_base.left)

print(tree_base.data)

self.middle_search(tree_base.right)

def behind_search(self,tree_base):

'後序遍歷:左-右-根'

if tree_base == 0:

return

self.behind_search(tree_base.left)

self.behind_search(tree_base.right)

print(tree_base.data)

非遞迴版本:

#前序遍歷

def preorder(self, root):

if root == none:

return

stack =

result =

node = root

while node or stack:

while node:

# 從根節點開始,一直找它的左子樹

node = node.left

# while結束表示當前節點node為空,即前乙個節點沒有左子樹了

node = stack.pop()

# 開始檢視它的右子樹

node = node.right

return result

#中序遍歷

def inorder(self,root):

if root == none:

return

stack =

result =

node = root

while node or stack:

while node:

# 從根節點開始,一直找到左子樹

node = node.left

# while結束表示當前節點node為空,即前乙個節點沒有左子樹了

node = stack.pop()

node = node.right

return result

#後序遍歷

def postorder(self, root):

if root == none:

return

stack =

result =

node = root

while node or stack:

while node:

# 從根節點開始,一直找它的右子樹

result.insert(0,node.val)

node = node.right

# while結束表示當前節點node為空,即前乙個節點沒有右子樹了

node = stack.pop()

# 開始檢視它的左子樹

node = node.left

return result

4、二叉樹兩節點最近公共祖先節點

class solution(object):

def lowestcommonancestor(self, root, p, q):

if(root is none or root==p or root==q):

return root

left=self.lowestcommonancestor(root.left,p,q)

right=self.lowestcommonancestor(root.right,p,q)

if left and right :

return root

if not left :

return right

if not right :

return left

廣度遍歷二叉樹和深度遍歷二叉樹演算法

二叉樹演算法基本和遞迴有關,前中後序演算法就不提了,主要看一下深度優先遍歷和廣度優先遍歷。實現這2種遍歷需要借助棧或者佇列來儲存中間結果,原因是遍歷過程出現了回溯。1 筆試題 廣度遍歷二叉樹 深度遍歷二叉樹 23 include4 include5 include6 7using namespace...

二叉樹層次遍歷 廣度遍歷和深度遍歷

問題 二叉樹層次遍歷 問題描述 給定乙個二叉樹,返回其按層次遍歷的節點值。即逐層地,從左到右訪問所有節點 例子 給定二叉樹 3,9,20,null,null,15,7 3 920 157返回其層次遍歷結果 3 9 20 15,7 思路 廣度遍歷和深度遍歷。與一般的廣度遍歷不同的是,每次遍歷都將que...

對稱二叉樹 深度遍歷與廣度遍歷

問題 對稱二叉樹 問題描述 給定乙個二叉樹,檢查它是否是映象對稱的。比如,下面這個二叉樹是映象對稱的 1 2 2 3 4 4 3 而這個二叉樹則不是映象對稱的。1 2 2 3 3例子 輸入 1,2,2,3,4,4,3 輸出 true 思路 映象對稱意味著左子樹與右子樹映象對稱,在遍歷左子樹或者右子樹...