From af6340c400999bb0dc2e26d09407ea18b5ef38fb Mon Sep 17 00:00:00 2001 From: nmydt <62681228+nmydt@users.noreply.github.com> Date: Wed, 19 May 2021 20:51:11 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 二叉树:以为使用了递归,其实还隐藏着回溯 Java代码 --- ...46\347\235\200\345\233\236\346\272\257.md" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index bf15e39b37..3f87433269 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -175,7 +175,85 @@ if (cur->right) { Java: + 100. 相同的树:递归代码 + ```java + class Solution { + public boolean compare(TreeNode tree1, TreeNode tree2) { + + if(tree1==null && tree2==null)return true; + if(tree1==null || tree2==null)return false; + if(tree1.val!=tree2.val)return false; + // 此时就是:左右节点都不为空,且数值相同的情况 + // 此时才做递归,做下一层的判断 + boolean compareLeft = compare(tree1.left, tree2.left); // 左子树:左、 右子树:左 + boolean compareRight = compare(tree1.right, tree2.right); // 左子树:右、 右子树:右 + boolean isSame = compareLeft && compareRight; // 左子树:中、 右子树:中(逻辑处理) + return isSame; + + } + boolean isSameTree(TreeNode p, TreeNode q) { + return compare(p, q); + } +} + ``` + 257. 二叉树的所有路径: 回溯代码 + ```java + class Solution { + public void traversal(TreeNode cur, List path, List result) { + path.add(cur.val); + // 这才到了叶子节点 + if (cur.left == null && cur.right == null) { + String sPath=""; + for (int i = 0; i < path.size() - 1; i++) { + sPath += ""+path.get(i); + sPath += "->"; + } + sPath += path.get(path.size() - 1); + result.add(sPath); + return; + } + if (cur.left!=null) { + traversal(cur.left, path, result); + path.remove(path.size()-1); // 回溯 + } + if (cur.right!=null) { + traversal(cur.right, path, result); + path.remove(path.size()-1); // 回溯 + } + } + + public List binaryTreePaths(TreeNode root) { + List result = new LinkedList<>(); + List path = new LinkedList<>(); + if (root == null) return result; + traversal(root, path, result); + return result; + } +} + + ``` + 如下为精简之后的递归代码:(257. 二叉树的所有路径) + ```java + class Solution { + public void traversal(TreeNode cur, String path, List result) { + path += cur.val; // 中 + if (cur.left == null && cur.right == null) { + result.add(path); + return; + } + if (cur.left!=null) traversal(cur.left, path + "->", result); // 左 回溯就隐藏在这里 + if (cur.right!=null) traversal(cur.right, path + "->", result); // 右 回溯就隐藏在这里 + } + public List binaryTreePaths(TreeNode root) { + List result = new LinkedList<>(); + String path = ""; + if (root == null) return result; + traversal(root, path, result); + return result; + } +} + ``` Python: