mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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 {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user