From 81501d2acfb12c49821e6f3348b52e2e10cde1c7 Mon Sep 17 00:00:00 2001 From: ironartisan <54694467+ironartisan@users.noreply.github.com> Date: Sun, 5 Sep 2021 15:31:39 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=9A=84=E6=97=B6=E9=97=B4=E4=B8=8E=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90.md?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../前序/递归算法的时间与空间复杂度分析.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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() From 429b504779d9981cd14329406ec9acbfd72cd76d Mon Sep 17 00:00:00 2001 From: ironartisan <54694467+ironartisan@users.noreply.github.com> Date: Sun, 5 Sep 2021 21:38:56 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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 From cae6b56b2ddda9abfc7f1116852cb11ad1e2d811 Mon Sep 17 00:00:00 2001 From: ironartisan <54694467+ironartisan@users.noreply.github.com> Date: Mon, 6 Sep 2021 10:50:21 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.mdJava=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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.路径总和 From 3450a85b6d1318ea5288dccd8475e57069c5a2aa Mon Sep 17 00:00:00 2001 From: ironartisan <54694467+ironartisan@users.noreply.github.com> Date: Mon, 6 Sep 2021 11:32:18 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=960106.=E4=BB=8E=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.mdJava?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0106.从中序与后序遍历序列构造二叉树.md | 1 + 1 file changed, 1 insertion(+) 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划分左右子树 From 2c584ce661768c35ac4e16f5af0cbbbf339b4bf4 Mon Sep 17 00:00:00 2001 From: ironartisan <54694467+ironartisan@users.noreply.github.com> Date: Mon, 6 Sep 2021 14:47:46 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00617.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=E9=98=9F=E5=88=97=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 39 +++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) 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