8000 Update README.md · abroroo/leetcode@da1b672 · GitHub
[go: up one dir, main page]

Skip to content

Commit da1b672

Browse files
authored
Update README.md
1 parent 8890b9f commit da1b672

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
0