20. 有效的括号
程序员文章站
2022-04-25 20:21:23
...
栈的基础使用
20. 有效的括号
// 20. Valid Parentheses
// https://leetcode.com/problems/valid-parentheses/description/
// 时间复杂度: O(n)
// 空间复杂度: O(n)
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
stack.push(s.charAt(i));
} else {
if (stack.size() == 0) {
return false;
}
Character c = stack.pop();
char match;
if (s.charAt(i) == ')') {
match = '(';
} else if (s.charAt(i) == ']') {
match = '[';
} else {
assert s.charAt(i) == '}';
match = '{';
}
if (!c.equals(match)) {
return false;
}
}
}
return stack.size() == 0;
}
//private static void printBool(boolean b) {
// System.out.println(b ? "True" : "False");
//}
//public static void main(String[] args) {
// printBool((new Solution()).isValid("()"));
// printBool((new Solution()).isValid("()[]{}"));
// printBool((new Solution()).isValid("(]"));
// printBool((new Solution()).isValid("([)]"));
//}
}
150. 逆波兰表达式求值
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
71. 简化路径
栈和递归的紧密关系
递归算法
二叉树中的算法
144. 二叉树的前序遍历
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
系统栈
使用栈模拟系统栈,写出非递归程序(三种遍历,几乎不用切换)
C++非递归
#include <vector>
#include <stack>
#include <cassert>
#include "iostream"
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//C++
struct Command {
string s;//go, print
TreeNode* node;
Command(string s, TreeNode *node) : s(s), node(node) {}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> res;
if (root == NULL) {
return res;
}
stack<Command> stack;
stack.push(Command("go", root));
while (!stack.empty()) {
//出栈
Command command = stack.top();
stack.pop();
if (command.s == "print") {
res.push_back(command.node->val);
} else {
assert (command.s == "go");
if (command.node->right) {
stack.push(Command("go", command.node->right));
}
if (command.node->left) {
stack.push(Command("go", command.node->left));
}
stack.push(Command("print", command.node));
}
}
return res;
}
};
JAVA 非递归
/// 144. Binary Tree Preorder Traversal
/// https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// 非递归二叉树的前序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
private class Command {
String s; // go, print
TreeNode node;
Command(String s, TreeNode node) {
this.s = s;
this.node = node;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
Stack<Command> stack = new Stack<Command>();
stack.push(new Command("go", root));
while (!stack.empty()) {
Command command = stack.pop();
if (command.s.equals("print")) {
res.add(command.node.val);
} else {
assert command.s.equals("go");
if (command.node.right != null) {
stack.push(new Command("go", command.node.right));
}
if (command.node.left != null) {
stack.push(new Command("go", command.node.left));
}
stack.push(new Command("print", command.node));
}
}
return res;
}
}
递归
// Definition for a binary tree node.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
/// 144. Binary Tree Preorder Traversal
/// https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// 二叉树的前序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
preorderTraversal(root, res);
return res;
}
private void preorderTraversal(TreeNode node, List<Integer> list) {
if (node != null) {
list.add(node.val);
preorderTraversal(node.left, list);
preorderTraversal(node.right, list);
}
}
}
94. 二叉树的中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
非递归
/// 94. Binary Tree Inorder Traversal
/// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
/// 非递归二叉树的中序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
private class Command {
String s; // go, print
TreeNode node;
Command(String s, TreeNode node) {
this.s = s;
this.node = node;
}
}
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
Stack<Command> stack = new Stack<Command>();
stack.push(new Command("go", root));
while (!stack.empty()) {
Command command = stack.pop();
if (command.s.equals("print")) {
res.add(command.node.val);
} else {
assert command.s.equals("go");
if (command.node.right != null) {
stack.push(new Command("go", command.node.right));
}
stack.push(new Command("print", command.node));
if (command.node.left != null) {
stack.push(new Command("go", command.node.left));
}
}
}
return res;
}
}
递归
/// 94. Binary Tree Inorder Traversal
/// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
/// 二叉树的中序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
inorderTraversal(root, res);
return res;
}
private void inorderTraversal(TreeNode node, List<Integer> list) {
if (node != null) {
inorderTraversal(node.left, list);
list.add(node.val);
inorderTraversal(node.right, list);
}
}
}
145. 二叉树的后序遍历
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
非递归
/// 145. Binary Tree Postorder Traversal
/// https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// 非递归的二叉树的后序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
private class Command {
String s; // go, print
TreeNode node;
Command(String s, TreeNode node) {
this.s = s;
this.node = node;
}
}
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
Stack<Command> stack = new Stack<Command>();
stack.push(new Command("go", root));
while (!stack.empty()) {
Command command = stack.pop();
if (command.s.equals("print")) {
res.add(command.node.val);
} else {
assert command.s.equals("go");
stack.push(new Command("print", command.node));
if (command.node.right != null) {
stack.push(new Command("go", command.node.right));
}
if (command.node.left != null) {
stack.push(new Command("go", command.node.left));
}
}
}
return res;
}
}
递归
/// 145. Binary Tree Postorder Traversal
/// https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// 二叉树的后序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
postorderTraversal(root, res);
return res;
}
private void postorderTraversal(TreeNode node, List<Integer> list) {
if (node != null) {
postorderTraversal(node.left, list);
postorderTraversal(node.right, list);
list.add(node.val);
}
}
}