Skip to content

Commit 8ff9138

Browse files
committedOct 5, 2021
Add solution #101
1 parent 7caccd0 commit 8ff9138

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
4242
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
4343
94|[Binary Tree Inorder Traversal](./0094-binary-tree-inorder-traversal.js)|Easy|
44+
101|[Symmetric Tree](./0101-symmetric-tree.js)|Easy|
4445
102|[Binary Tree Level Order Traversal](./0102-binary-tree-level-order-traversal.js)|Medium|
4546
104|[Maximum Depth of Binary Tree](./0104-maximum-depth-of-binary-tree.js)|Easy|
4647
111|[Minimum Depth of Binary Tree](./0111-minimum-depth-of-binary-tree.js)|Easy|

‎solutions/0101-symmetric-tree.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 101. Symmetric Tree
3+
* https://leetcode.com/problems/symmetric-tree/
4+
* Difficulty: Easy
5+
*
6+
* Given the root of a binary tree, check whether it is a mirror
7+
* of itself (i.e., symmetric around its center).
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* function TreeNode(val, left, right) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.left = (left===undefined ? null : left)
15+
* this.right = (right===undefined ? null : right)
16+
* }
17+
*/
18+
/**
19+
* @param {TreeNode} root
20+
* @return {boolean}
21+
*/
22+
var isSymmetric = function(root) {
23+
return isBalanced(root.left, root.right);
24+
};
25+
26+
function isBalanced(a, b) {
27+
if (!a && !b) {
28+
return true;
29+
}
30+
if (!a || !b) {
31+
return false;
32+
}
33+
return a.val === b.val && isBalanced(a.left, b.right) && isBalanced(a.right, b.left);
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.