代码随想录
LeetCode 题解
1、二叉树的遍历

1.1、前序遍历
144. 二叉树的前序遍历
| public List<Integer> preorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| preorder(root, list); |
| return list; |
| } |
| |
| private void preorder(TreeNode node, List<Integer> list) { |
| if (node == null) return; |
| |
| list.add(node.val); |
| preorder(node.left, list); |
| preorder(node.right, list); |
| } |
| public List<Integer> preorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Deque<TreeNode> stack = new LinkedList<>(); |
| stack.push(root); |
| while (!stack.isEmpty()) { |
| TreeNode cur = stack.pop(); |
| |
| list.add(cur.val); |
| if (cur.right != null) stack.push(cur.right); |
| if (cur.left != null) stack.push(cur.left); |
| } |
| |
| return list; |
| } |
| private class Command { |
| public String s; |
| public TreeNode node; |
| |
| public Command(String s, TreeNode node) { |
| this.s = s; |
| this.node = node; |
| } |
| } |
| |
| public List<Integer> preorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Deque<Command> stack = new LinkedList<>(); |
| stack.push(new Command("go", root)); |
| while (!stack.isEmpty()) { |
| Command command = stack.pop(); |
| String s = command.s; |
| TreeNode node = command.node; |
| |
| if (s.equals("print")) list.add(node.val); |
| else { |
| if (node.right != null) stack.push(new Command("go", node.right)); |
| if (node.left != null) stack.push(new Command("go", node.left)); |
| stack.push(new Command("print", node)); |
| } |
| } |
| |
| return list; |
| } |
1.2、中序遍历
94. 二叉树的中序遍历
| public List<Integer> inorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| inorder(root, list); |
| return list; |
| } |
| |
| private void inorder(TreeNode node, List<Integer> list) { |
| if (node == null) return; |
| |
| inorder(node.left, list); |
| list.add(node.val); |
| inorder(node.right, list); |
| } |
| public List<Integer> inorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Deque<TreeNode> stack = new LinkedList<>(); |
| TreeNode cur = root; |
| while (cur != null || !stack.isEmpty()) { |
| if (cur != null) { |
| stack.push(cur); |
| cur = cur.left; |
| } else { |
| cur = stack.pop(); |
| list.add(cur.val); |
| cur = cur.right; |
| } |
| } |
| |
| return list; |
| } |
| private class Command { |
| public String s; |
| public TreeNode node; |
| |
| public Command(String s, TreeNode node) { |
| this.s = s; |
| this.node = node; |
| } |
| } |
| |
| public List<Integer> inorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Deque<Command> stack = new LinkedList<>(); |
| stack.push(new Command("go", root)); |
| while (!stack.isEmpty()) { |
| Command command = stack.pop(); |
| String s = command.s; |
| TreeNode node = command.node; |
| |
| if (s.equals("print")) list.add(node.val); |
| else { |
| if (node.right != null) stack.push(new Command("go", node.right)); |
| stack.push(new Command("print", node)); |
| if (node.left != null) stack.push(new Command("go", node.left)); |
| } |
| } |
| |
| return list; |
| } |
1.3、后序遍历
145. 二叉树的后序遍历
| public List<Integer> postorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| postorder(root, list); |
| return list; |
| } |
| |
| private void postorder(TreeNode node, List<Integer> list) { |
| if (node == null) return; |
| |
| postorder(node.left, list); |
| postorder(node.right, list); |
| list.add(node.val); |
| } |
| public List<Integer> postorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Deque<TreeNode> stack1 = new LinkedList<>(); |
| Deque<TreeNode> stack2 = new LinkedList<>(); |
| stack1.push(root); |
| while (!stack1.isEmpty()) { |
| TreeNode cur = stack1.pop(); |
| |
| stack2.push(cur); |
| if (cur.left != null) stack1.push(cur.left); |
| if (cur.right != null) stack1.push(cur.right); |
| } |
| while (!stack2.isEmpty()) list.add(stack2.pop().val); |
| |
| return list; |
| } |
| private class Command { |
| public String s; |
| public TreeNode node; |
| |
| public Command(String s, TreeNode node) { |
| this.s = s; |
| this.node = node; |
| } |
| } |
| |
| public List<Integer> postorderTraversal(TreeNode root) { |
| List<Integer> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Deque<Command> stack = new LinkedList<>(); |
| stack.push(new Command("go", root)); |
| while (!stack.isEmpty()) { |
| Command command = stack.pop(); |
| String s = command.s; |
| TreeNode node = command.node; |
| |
| if (s.equals("print")) list.add(node.val); |
| else { |
| stack.push(new Command("print", node)); |
| if (node.right != null) stack.push(new Command("go", node.right)); |
| if (node.left != null) stack.push(new Command("go", node.left)); |
| } |
| } |
| |
| return list; |
| } |
1.4、层序遍历
102. 二叉树的层序遍历
| public List<List<Integer>> levelOrder(TreeNode root) { |
| List<List<Integer>> list = new ArrayList<>(); |
| if (root == null) return list; |
| |
| Queue<TreeNode> queue = new LinkedList<>(); |
| queue.add(root); |
| while (!queue.isEmpty()) { |
| int size = queue.size(); |
| List<Integer> level = new ArrayList<>(); |
| |
| for (int i = 0; i < size; i++) { |
| TreeNode cur = queue.remove(); |
| level.add(cur.val); |
| if (cur.left != null) queue.add(cur.left); |
| if (cur.right != null) queue.add(cur.right); |
| } |
| |
| list.add(level); |
| } |
| |
| return list; |
| } |
2、二叉树的属性
3、二叉树的修改与构造
4、二叉搜索树的属性
5、二叉树公共祖先
6、二叉树搜索的修改与构造
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步