From 95fa89013dd6393f4cf324184ff4c898b609d23e Mon Sep 17 00:00:00 2001 From: Copyes Date: Sun, 14 Jun 2020 21:59:39 +0800 Subject: [PATCH] bug fixed --- ScriptOJ/isBalanced.js | 2 +- ScriptOJ/isSymmetric.js | 23 +++++++++++++++++++ .../\344\272\214\345\217\211\346\240\221.md" | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 ScriptOJ/isSymmetric.js diff --git a/ScriptOJ/isBalanced.js b/ScriptOJ/isBalanced.js index c784781..d1316ea 100644 --- a/ScriptOJ/isBalanced.js +++ b/ScriptOJ/isBalanced.js @@ -17,7 +17,7 @@ var isBalanced = function(root) { if(node === null) return 0; let left = dfs(node.left); let right = dfs(node.right); - if(Math.abs(left, right) > 1) { + if(Math.abs(left-right) > 1) { ans = false; } return Math.max(left, right) + 1; diff --git a/ScriptOJ/isSymmetric.js b/ScriptOJ/isSymmetric.js new file mode 100644 index 0000000..e8a6822 --- /dev/null +++ b/ScriptOJ/isSymmetric.js @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isSymmetric = function(root) { + if(root == null) return true; + + function recur(L, R) { + if(L === null && R === null) return true; + if(L === null || R === null || L.val !== R.val) return false; + + return recur(L.left, R.right) && recur(L.right, R.left); + } + + return recur(root.left, root.right); +}; \ No newline at end of file diff --git "a/Solutions/leetcode-solution/\344\272\214\345\217\211\346\240\221.md" "b/Solutions/leetcode-solution/\344\272\214\345\217\211\346\240\221.md" index a82b4a9..b38d8cf 100644 --- "a/Solutions/leetcode-solution/\344\272\214\345\217\211\346\240\221.md" +++ "b/Solutions/leetcode-solution/\344\272\214\345\217\211\346\240\221.md" @@ -73,3 +73,4 @@ var postorderTraversal = function(root) { ### 题目列表 - [x] 112. 路径总和 https://leetcode-cn.com/problems/path-sum/ - [x] 面试题27. 二叉树的镜像 https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/ +- [x] 面试题28. 对称的二叉树 https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/