diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 8ae7ee79..220232a2 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2532,20 +2532,18 @@ class Solution: return 0 queue_ = [root] - result = [] + depth = 0 while queue_: length = len(queue_) - sub = [] for i in range(length): cur = queue_.pop(0) sub.append(cur.val) #子节点入队列 if cur.left: queue_.append(cur.left) if cur.right: queue_.append(cur.right) - result.append(sub) + depth += 1 - - return len(result) + return depth ``` Go: diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 176ea687..f0323bc4 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -163,6 +163,25 @@ class Solution { python3 +```python +from operator import add, sub, mul + +class Solution: + op_map = {'+': add, '-': sub, '*': mul, '/': lambda x, y: int(x / y)} + + def evalRPN(self, tokens: List[str]) -> int: + stack = [] + for token in tokens: + if token not in {'+', '-', '*', '/'}: + stack.append(int(token)) + else: + op2 = stack.pop() + op1 = stack.pop() + stack.append(self.op_map[token](op1, op2)) # 第一个出来的在运算符后面 + return stack.pop() +``` + +另一种可行,但因为使用eval相对较慢的方法: ```python class Solution: def evalRPN(self, tokens: List[str]) -> int: diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index b87877c9..10ac8264 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -188,7 +188,7 @@ public: ```CPP if (root == nullptr) return 0; -// 开始根据做深度和有深度是否相同来判断该子树是不是满二叉树 +// 开始根据左深度和右深度是否相同来判断该子树是不是满二叉树 TreeNode* left = root->left; TreeNode* right = root->right; int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便 @@ -843,3 +843,4 @@ impl Solution { +