leetcode-226-234

leetcode226题234题:

leetcode226题(翻转二叉树):

翻转一棵二叉树。

示例:

示例

解答:
1
2
3
4
5
6
7
8
9
10
11
12
var invertTree = function(root) {
if(root==null){
return root;
}
var temp = new TreeNode(null);
temp = root.left;
root.left = root.right;
root.right = temp;
root.left = invertTree(root.left);
root.right = invertTree(root.right);
return root;
};

leetcode234题:

请判断一个链表是否为回文链表。

示例:

输入:1->2

输出:false

输入:1->2->2->1

输出:true

解答:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var isPalindrome = function(head) {
if(head==null||head.next==null){
return true;
}
var judgeArr = [];
var count = 0;
var head1 = head;
while(head1!=null){
count++;
head1 = head1.next;
}
while(head!=null){
judgeArr.push(head.val);
if((count%2==0)&&(head.next!=null)&&(head.val==head.next.val)){
head = head.next;
break;
}
if((count%2==1)&&(head.next!=null)&&(head.next.next!=null)&&(head.val==head.next.next.val)){
head = head.next.next;
break;
}
head = head.next;
}
while(head!=null){
if(head.val!=judgeArr.pop()){
return false;
}
head = head.next;
}
if(judgeArr.length==0){
return true;
}
return false;
};
分享到