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.
Basically, I would like to solve the easy acceptance in descending order.
Last time Leet Code Day7 starting from zero "104. Maximum Depth of Binary Tree"
This is the first Medium. This problem also has a large number of good, so I thought it was a good problem, so I tried to solve it.
The problem is very simple, given the binary tree, it returns the total value of the deepest leaves. ~~ I feel that there are many tree-related problems these days ... ~~
An example is as follows
Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8] Output: 15
In this case, the deepest leaves are 7 and 8, so add up to 15.
At first glance at the problem, I thought it would be a simple idea to use a depth-first search to check the depth of all the leaves and return the depth and value. However, I felt that it would be a little redundant code, and if you use a hash table at the same time when checking the depth, you can hold both the depth and the value and write it efficiently? I thought and implemented it.
# 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 deepestLeavesSum(self, root: TreeNode) -> int:
ans = {}
count = 0
self.dfs(root,ans,count)
return ans[max(ans)]
def dfs(self,node,ans,count):
if node:
if node.left:
self.dfs(node.left,ans,count+1)
if node.right:
self.dfs(node.right,ans,count+1)
if count not in ans:
ans[count] = node.val
else:
ans[count] += node.val
# Runtime: 104 ms, faster than 46.02% of Python3 online submissions for Deepest Leaves Sum.
# Memory Usage: 17.4 MB, less than 100.00% of Python3 online submissions for Deepest Leaves Sum.
In the dfs function, if it is equal to the index of count, it is added, otherwise, the value is assigned as it is. If you return the maximum value in ans, the value of the deepest leaf will be returned in the end. But this answer isn't very fast ...
I think the following answers are the fastest in Python in the range of observing discuss.
# 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 deepestLeavesSum(self, root):
q = [root]
while q:
pre, q = q, [child for p in q for child in [p.left, p.right] if child]
return sum(node.val for node in pre)
# Runtime: 84 ms, faster than 98.04% of Python3 online submissions for Deepest Leaves Sum.
# Memory Usage: 17.3 MB, less than 100.00% of Python3 online submissions for Deepest Leaves Sum.
But it's really fast ... I don't think I can write such code at the moment, but it's very interesting to be able to read such code.
This made it so fast! If you have an example, I would appreciate it if you could tell me what you devised by writing the code, memory consumption and runtime in the comment section.
Recommended Posts