Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 57bfaa3

Browse files
101. Symmetric Tree
1 parent 5952bbd commit 57bfaa3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎src/101. Symmetric Tree.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 101. Symmetric Tree
3+
* https://leetcode.com/problems/symmetric-tree/
4+
* Definition for a binary tree node.
5+
* function TreeNode(val) {
6+
* this.val = val;
7+
* this.left = this.right = null;
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @return {boolean}
13+
*/
14+
var isSymmetric = function (root) {
15+
if (!root || (!root.left && !root.right)) {
16+
return true;
17+
}
18+
return s(root.left, root.right);
19+
};
20+
21+
function s(left, right) {
22+
if (!left && !right) {
23+
return true;
24+
}
25+
if (!left || !right) {
26+
return false;
27+
}
28+
return left.val === right.val && s(left.right, right.left) && s(left.left, right.right);
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /