Binary Tree based Recursion & Divide Conquer 二叉树递归与分治
226. Invert Binary Tree (Easy)
Invert a binary tree.
1 | Example: |
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
递归1:
1 | class Solution: |
递归2:
1 | class Solution: |
递归3:
1 | class Solution: |
遍历:
1 | class Solution: |
100. Same Tree (Easy)
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
1 | Example 1: |
递归:
1 | class Solution: |
遍历:
1 | class Solution: |
五刷:高频
101. Symmetric Tree (Easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
1 | For example, this binary tree [1,2,2,3,4,4,3] is symmetric: |
Note:
Bonus points if you could solve it both recursively and iteratively.
递归
1 | class Solution: |
遍历1:
1 | class Solution: |
遍历2:
1 | class Solution: |
8刷:高频