diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 39b55d8c..a9639d8f 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -607,6 +607,7 @@ class Solution { for (int i = inLeft; i < inRight; i++) { if (inorder[i] == rootVal) { rootIndex = i; + break; } } // 根据rootIndex划分左右子树 diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 283a9913..da62452c 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -411,6 +411,31 @@ class solution { } ``` +```java +// 解法2 +class Solution { + List> result; + LinkedList path; + public List> pathSum (TreeNode root,int targetSum) { + result = new LinkedList<>(); + path = new LinkedList<>(); + travesal(root, targetSum); + return result; + } + private void travesal(TreeNode root, int count) { + if (root == null) return; + path.offer(root.val); + count -= root.val; + if (root.left == null && root.right == null && count == 0) { + result.add(new LinkedList<>(path)); + } + travesal(root.left, count); + travesal(root.right, count); + path.removeLast(); // 回溯 + } +} +``` + ## python 0112.路径总和 diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 2984427f..6386c48d 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -367,6 +367,43 @@ class Solution { } } ``` +```java +// 解法2 +class Solution { + /** + * 迭代法 + */ + public List binaryTreePaths(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) + return result; + Stack stack = new Stack<>(); + // 节点和路径同时入栈 + stack.push(root); + stack.push(root.val + ""); + while (!stack.isEmpty()) { + // 节点和路径同时出栈 + String path = (String) stack.pop(); + TreeNode node = (TreeNode) stack.pop(); + // 若找到叶子节点 + if (node.left == null && node.right == null) { + result.add(path); + } + //右子节点不为空 + if (node.right != null) { + stack.push(node.right); + stack.push(path + "->" + node.right.val); + } + //左子节点不为空 + if (node.left != null) { + stack.push(node.left); + stack.push(path + "->" + node.left.val); + } + } + return result; + } +} +``` Python: ```Python diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 22a527f9..e21efcb3 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -274,7 +274,7 @@ class Solution { ```Java class Solution { - // 迭代 + // 使用栈迭代 public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if (root1 == null) { return root2; @@ -310,6 +310,43 @@ class Solution { } } ``` +```java +class Solution { + // 使用队列迭代 + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + if (root1 == null) return root2; + if (root2 ==null) return root1; + Queue queue = new LinkedList<>(); + queue.offer(root1); + queue.offer(root2); + while (!queue.isEmpty()) { + TreeNode node1 = queue.poll(); + TreeNode node2 = queue.poll(); + // 此时两个节点一定不为空,val相加 + node1.val = node1.val + node2.val; + // 如果两棵树左节点都不为空,加入队列 + if (node1.left != null && node2.left != null) { + queue.offer(node1.left); + queue.offer(node2.left); + } + // 如果两棵树右节点都不为空,加入队列 + if (node1.right != null && node2.right != null) { + queue.offer(node1.right); + queue.offer(node2.right); + } + // 若node1的左节点为空,直接赋值 + if (node1.left == null && node2.left != null) { + node1.left = node2.left; + } + // 若node2的左节点为空,直接赋值 + if (node1.right == null && node2.right != null) { + node1.right = node2.right; + } + } + return root1; + } +} +``` ## Python diff --git a/problems/前序/递归算法的时间与空间复杂度分析.md b/problems/前序/递归算法的时间与空间复杂度分析.md index 27b9d7cc..7a690781 100644 --- a/problems/前序/递归算法的时间与空间复杂度分析.md +++ b/problems/前序/递归算法的时间与空间复杂度分析.md @@ -166,7 +166,7 @@ void time_consumption() { system_clock::now().time_since_epoch() ); - fibonacci_3(0, 1, n); + fibonacci_3(1, 1, n); milliseconds end_time = duration_cast( system_clock::now().time_since_epoch()