Binary tree traversal python3 codes (the first order, in sequence, after, levels) (recursive, non-recursive)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

 

(A) a binary tree preorder

Recursion:

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]: res=[] if root: res+=self.inorderTraversal(root.left) res.append(root.val) res+=self.inorderTraversal(root.right) return res
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]: if not root: return [] return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)

Note:

1. Self-calling class method

2. Python in the list can be directly obtained by adding a new list:

ls1 = [1,2,3]
ls2 = [4,5,6] print(ls1+ls2)

Iteration:

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]: # 迭代解法 p = root res = [] stack = [] while p or stack: if p: stack.append(p) p = p.left else: tmp = stack.pop() res.append(tmp.val); p = tmp.right return res 

Preorder (b) a binary tree (preamble) traversal

Recursion:

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]: '''递归解法''' p =root res = [] if p!=None: res.append(p.val) res += self.preorderTraversal(p.left) res += self.preorderTraversal(p.right) return res

Iteration:

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]: '''迭代解法''' p = root res = [] stack = [] while p or stack: if p: res.append(p.val) stack.append(p) p = p.left else: temp = stack.pop() p = temp.right return res

After the sequence (iii) the binary tree traversal

Recursion:

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]: p = root res = [] if p: res += self.postorderTraversal(p.left) res += self.postorderTraversal(p.right) res.append(p.val) return res

 

Postorder References

Has been explained in detail, it will not be described.

Iteration 1:

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]: '''先序遍历思想实现后续遍历''' p = root #res = [] stack = [] stack2 = [] while p or stack: if p: stack2.append(p.val) stack.append(p) p = p.right else: temp = stack.pop() p = temp.left return stack2[::-1] 

Iteration 2:

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]: '''后序遍历双指针迭代算法''' if not root: # 需要判断是否为空 return [] stack = [] res = [] prev = None curr = None stack.append(root) while stack: curr = stack[-1] if prev==None or prev.left==curr or prev.right==curr: if curr.left!=None: stack.append(curr.left) elif curr.right!=None: stack.append(curr.right) elif prev == curr.left: if curr.right!=None: stack.append(curr.right) else: res.append(curr.val) stack.pop() # 需要弹出 prev = curr return res 

(Iv) the level of binary tree traversal

Structure cohort

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]: if not root: return [] queue = [] res = [] p = root queue.append(p) while queue: temp = queue.pop(0) res.append(temp.val) if temp.left!=None: queue.append(temp.left) if temp.right!=None: queue.append(temp.right) return res

Guess you like

Origin www.cnblogs.com/ACStrive/p/11222390.html