二叉树的递归遍历----以leetcode例题为例
树的递归遍历核心是:root节点做什么,孩子节点做相同的事情
一、入门简单
1、LeetCode94. 二叉树的中序遍历
左根右
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def inorderTraversal(self, root: TreeNode) -> List[int]: 9 ans=[] 10 def inorder(root): 11 if root==None: 12 return 13 inorder(root.left) 14 ans.append(root.val) 15 inorder(root.right) 16 inorder(root) 17 return ans
二、中等难度
1、leetcode 226.翻转二叉树
root节点:让左右节点翻转,孩子节点也翻转
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def invertTree(self, root: TreeNode) -> TreeNode: 9 if(root==None): 10 return None 11 tmp=root.left 12 root.left=root.right 13 root.right=tmp 14 self.invertTree(root.left) 15 self.invertTree(root.right) 16 17 return root
2、leetcode 654.最大二叉树
root 找到最大值为root值,最大值左边建造左儿子们,最大值右边建造右儿子们
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: 9 if len(nums)==0: 10 return None 11 large=-1 12 indx=-1 13 14 for i in range(len(nums)): 15 if nums[i]>large: 16 large=nums[i] 17 indx=i 18 root = TreeNode(large) 19 root.left=self.constructMaximumBinaryTree(nums[:indx]) 20 root.right=self.constructMaximumBinaryTree(nums[indx+1:]) 21 return root
3、leetcode 116.填充每个节点的下一个右侧节点指针
root这里做的事什么
1 """ 2 # Definition for a Node. 3 class Node: 4 def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): 5 self.val = val 6 self.left = left 7 self.right = right 8 self.next = next 9 """ 10 11 class Solution: 12 def connect(self, root: 'Node') -> 'Node': 13 if(root==None): 14 return None 15 self.connectAdjacent(root.left, root.right) 16 return root 17 18 def connectAdjacent(self, n1:'Node', n2:'Node')->'Node': 19 if n1==None or n2 ==None: 20 return 21 n1.next=n2 22 self.connectAdjacent(n1.left, n1.right) 23 self.connectAdjacent(n1.right,n2.left) 24 self.connectAdjacent(n2.left,n2.right)
4、leetcode 114. 二叉树展开为链表
先序遍历,左子树的记录保存
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 void flatten(TreeNode* root) { 15 if(root==nullptr){ 16 return; 17 } 18 flatten(root->left); 19 flatten(root->right); 20 TreeNode* left=root->left; 21 TreeNode* right=root->right; 22 root->left=nullptr; 23 root->right=left; 24 25 TreeNode* p=root; 26 while(p->right!=nullptr){ 27 p=p->right; 28 } 29 p->right=right; 30 } 31 };
5、105. 从前序与中序遍历序列构造二叉树
对于root,根据前序遍历找出root的值,找出root的值在中序遍历中的位置,分段左右子树
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode: 9 return self.build(preorder, 0, len(preorder)-1, inorder, 0, len(inorder)-1) 10 def build(self, preorder, pStart, pEnd, inorder, iStart, iEnd): 11 if (pStart>pEnd): 12 return None 13 root=TreeNode(preorder[pStart]) 14 indx=inorder.index(preorder[pStart]) 15 leftsize=indx-iStart 16 root.left=self.build(preorder, pStart+1, pStart+leftsize, inorder, iStart, indx-1) 17 root.right=self.build(preorder, pStart+leftsize+1, pEnd, inorder, indx+1, iEnd) 18 return root
6、106. 从中序与后序遍历序列构造二叉树
对于root,对比前面的前序和中序
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: 9 return self.build(inorder,0, len(inorder)-1, postorder,0, len(postorder)-1) 10 11 def build(self, inorder, istart, iend, postorder, pstart, pend): 12 if istart>iend: 13 return None 14 root=TreeNode(postorder[pend]) 15 indx=inorder.index(postorder[pend]) 16 leftsize=indx-istart 17 root.left=self.build(inorder, istart,indx-1, postorder,pstart,pstart+leftsize-1) 18 root.right=self.build(inorder,indx+1, iend, postorder, pstart+leftsize, pend-1) 19 return root
7、652. 寻找重复的子树
对于每个节点,找出完整形状,并保存起来
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]: 9 memo=collections.Counter() 10 res=[] 11 self.traverse(root, memo,res) 12 return res 13 14 def traverse(self, root,memo,res): 15 if(root==None): 16 return '#' 17 left=self.traverse(root.left,memo,res) 18 right=self.traverse(root.right,memo,res) 19 20 subTree=str(left)+','+str(right)+','+str(root.val) 21 memo[subTree]+=1 22 if memo[subTree]==2: 23 res.append(root) 24 return subTree