It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
As a countermeasure, it seems that a site called Let Code will take measures.
A site that trains algorithmic power that can withstand coding tests that are often done in the home.
I think it's better to have the algorithm power of a human being, so I'll solve the problem irregularly and write down the method I thought at that time as a memo.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day 26 starting from zero "94. Binary Tree Inorder Traversal"
Basically, I would like to solve the easy acceptance in descending order.
Twitter I'm doing it.
101. Symmetric Tree The difficulty level is easy. Excerpt from Top 100 Liked Questions.
The problem is that a binary tree is given, so make sure it is symmetrical.
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
return self.dfs(root,root)
def dfs(self,left,right):
if left and right:
return left.val == right.val and self.dfs(left.left,right.right) and self.dfs(left.right,right.left)
else:
return left == right
# Runtime: 32 ms, faster than 71.25% of Python3 online submissions for Symmetric Tree.
# Memory Usage: 13.9 MB, less than 5.17% of Python3 online submissions for Symmetric Tree.
Solved by depth-first search.
Keep in mind that you have to determine if the symmetry is right and left. I don't write much ...
If there is a good answer, I will add it.
Recommended Posts