无论如何要在 Java 中复制不同元素的元组?
Anyway to replicate a tuple of different elements in Java?
考虑二叉树的最大深度问题,然后迭代求解。问题:https://leetcode.com/problems/maximum-depth-of-binary-tree/
这就是我在 python
中的做法
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if(root == None):
return 0
stack = [(root, 1)] # (treeNode, depth)
max_depth = 1
while(stack != []):
node, depth = stack.pop()
if(node.left):
stack.append((node.left, depth + 1))
if(node.right):
stack.append((node.right, depth + 1))
max_depth = max(max_depth, depth)
return max_depth
这就是我在 Java
中的做法
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
Stack <TreeNode> nodes = new Stack <>();
Stack <Integer> countNodes = new Stack <>();
int max = 1;
nodes.push(root);
countNodes.push(1);
while (!nodes.isEmpty()) {
TreeNode cur = nodes.pop();
int curNodeCount = countNodes.pop();
if (cur.left != null) {
nodes.push(cur.left);
countNodes.push(curNodeCount + 1);
}
if (cur.right != null) {
nodes.push(cur.right);
countNodes.push(curNodeCount + 1);
}
max = Math.max(max, curNodeCount);
}
return max;
}
只是想知道有没有办法制作一个元组 (TreeNode node, int depth) 这样我就可以避免在 Java 中制作两个堆栈? (就像我在 python 中所做的那样)。
您可以使用配对 class:
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
Stack<Pair<TreeNode, Integer>> stack = new Stack<>();
int max = 1;
stack.push(new Pair<>(root,1));
while (!stack.isEmpty()) {
Pair<TreeNode, Integer> curPair = stack.pop();
TreeNode cur = curPair.getKey();
int curNodeCount = curPair.getValue();
if (cur.left != null)
stack.push(new Pair<>(cur.left, curNodeCount + 1));
if (cur.right != null)
stack.push(new Pair<>(cur.right, curNodeCount + 1));
max = Math.max(max, curNodeCount);
}
return max;
}
由于 Java 中没有元组的概念,因此您可以在 Java 中使用这些方法。也就是说,您的 Stack
现在将包含其中之一的实例,而不是每个元素一个 Stack
。
使用包含必填字段的 class
- 像这样创建一个class:
public class YourClass{
private TreeNode node;
private int depth;
//Getters, Setters
}
- 使用此 class 的实例作为您的“元组”
使用对象[]
- 在这里您创建一个
Object[]
所需大小(在本例中为 2)并为其设置值
Object[] tuple = new Object[]{ treeNode, depth };
- 使用索引访问元素
使用 Map
当然,这并不是真正的元组。但对于很多这样的场景来说,这是一个很好的解决方案。
- 使用命名键创建映射并将值放入其中
Map<String, Object> tuple = new HashMap<>();
tuple.put( "treeNode", treeNode );
tuple.put( "depth", depth );
- 使用键访问元素
考虑二叉树的最大深度问题,然后迭代求解。问题:https://leetcode.com/problems/maximum-depth-of-binary-tree/
这就是我在 python
中的做法def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if(root == None):
return 0
stack = [(root, 1)] # (treeNode, depth)
max_depth = 1
while(stack != []):
node, depth = stack.pop()
if(node.left):
stack.append((node.left, depth + 1))
if(node.right):
stack.append((node.right, depth + 1))
max_depth = max(max_depth, depth)
return max_depth
这就是我在 Java
中的做法public int maxDepth(TreeNode root) {
if (root == null)
return 0;
Stack <TreeNode> nodes = new Stack <>();
Stack <Integer> countNodes = new Stack <>();
int max = 1;
nodes.push(root);
countNodes.push(1);
while (!nodes.isEmpty()) {
TreeNode cur = nodes.pop();
int curNodeCount = countNodes.pop();
if (cur.left != null) {
nodes.push(cur.left);
countNodes.push(curNodeCount + 1);
}
if (cur.right != null) {
nodes.push(cur.right);
countNodes.push(curNodeCount + 1);
}
max = Math.max(max, curNodeCount);
}
return max;
}
只是想知道有没有办法制作一个元组 (TreeNode node, int depth) 这样我就可以避免在 Java 中制作两个堆栈? (就像我在 python 中所做的那样)。
您可以使用配对 class:
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
Stack<Pair<TreeNode, Integer>> stack = new Stack<>();
int max = 1;
stack.push(new Pair<>(root,1));
while (!stack.isEmpty()) {
Pair<TreeNode, Integer> curPair = stack.pop();
TreeNode cur = curPair.getKey();
int curNodeCount = curPair.getValue();
if (cur.left != null)
stack.push(new Pair<>(cur.left, curNodeCount + 1));
if (cur.right != null)
stack.push(new Pair<>(cur.right, curNodeCount + 1));
max = Math.max(max, curNodeCount);
}
return max;
}
由于 Java 中没有元组的概念,因此您可以在 Java 中使用这些方法。也就是说,您的 Stack
现在将包含其中之一的实例,而不是每个元素一个 Stack
。
使用包含必填字段的 class
- 像这样创建一个class:
public class YourClass{
private TreeNode node;
private int depth;
//Getters, Setters
}
- 使用此 class 的实例作为您的“元组”
使用对象[]
- 在这里您创建一个
Object[]
所需大小(在本例中为 2)并为其设置值
Object[] tuple = new Object[]{ treeNode, depth };
- 使用索引访问元素
使用 Map
当然,这并不是真正的元组。但对于很多这样的场景来说,这是一个很好的解决方案。
- 使用命名键创建映射并将值放入其中
Map<String, Object> tuple = new HashMap<>();
tuple.put( "treeNode", treeNode );
tuple.put( "depth", depth );
- 使用键访问元素