@@ -4,6 +4,41 @@ List is periodically updated
44
55## LeetCode
66
7+ ### #101 Symmetric Tree
8+
9+ ```
10+
11+ /**
12+ * Definition for a binary tree node.
13+ * function TreeNode(val, left, right) {
14+ * this.val = (val===undefined ? 0 : val)
15+ * this.left = (left===undefined ? null : left)
16+ * this.right = (right===undefined ? null : right)
17+ * }
18+ */
19+ /**
20+ * @param {TreeNode} root
21+ * @return {boolean}
22+ */
23+ const isSymmetric = function(root) {
24+ // tree with null root considered symmetric
25+ if (root == null) return true
26+ // check if same first two nodes of the root
27+ return isSame(root.left, root.right)
28+ };
29+
30+ const isSame = (leftN, rightN) => {
31+ // if both nodes are null then tree symmetric
32+ if (leftN == null && rightN == null) return true
33+ // if only one of them null, no longer symmetric
34+ if (leftN == null || rightN == null) return false
35+ // if two values are not equal , not symmetric
36+ if (leftN.val !== rightN.val) return false
37+ // recursivly check both outer sides of the seconf node and inner sides respectively all over the height of the tree
38+ return isSame(leftN.left, rightN.right) && isSame(leftN.right, rightN.left);
39+ }
40+
41+ ```
742### #26 Remove Duplicate From Sorted Array
843
944```
0 commit comments