第十六题 合并两个排序的链表
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
方法一:递归
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function Merge(pHead1, pHead2)
{
// write code here
var newList;
if (pHead1 === null) return pHead2;
if (pHead2 === null) return pHead1;
if(pHead1.val > pHead2.val){
newList = pHead2;
newList.next = Merge(pHead1, pHead2.next);
} else {
newList = pHead1;
newList.next = Merge(pHead1.next, pHead2);
}
return newList;
}
方法二:非递归
function Merge(pHead1, pHead2)
{
// write code here
if (pHead1 === null) return pHead2;
if (pHead2 === null) return pHead1;
var head = null; //新链表表头
if(pHead1.val > pHead2.val){
head = pHead2;
pHead2 = pHead2.next;
} else {
head = pHead1;
pHead1 = pHead1.next;
}
var current = head; // 再用一个指针来移动
while (pHead1 !== null && pHead2 !== null){
// 将值小的节点赋给新链表(通过current.next)
if(pHead1.val > pHead2.val){
current.next = pHead2;
current = current.next;
pHead2 = pHead2.next;
} else {
current.next = pHead1;
current = current.next;
pHead1 = pHead1.next;
}
}
if (pHead1 != null) current.next = pHead1;
if (pHead2 != null) current.next = pHead2;
return head;
}
运行时间:12ms,占用内存:5348k
第十七题 树的子结构
题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
分析
首先:明确什么是树的子结构
树的子结构就是和该树的某个部分相同的树,子结构的意思是包含了一个结点,可以只取左子树或者右子树,或者都不取,且取左子树或右子树时不必取得整个左子树或者右子树。
- 第一步在树A中找到和B的根节点的值一样的结点R;
- 第二步再判断树A中以R为根结点的子树是不是包含和树B一样的结构。
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function HasSubtree(pRoot1, pRoot2)
{
// write code here
var result = false; // pRoot1 或 pRoot2 为空则返回false
if (pRoot1 !== null && pRoot2 !== null) {
if (pRoot1.val === pRoot2.val)
// 当节点值相同,用一个函数去判断它们的结构是否相同
result = doesTree1HaveTree2(pRoot1,pRoot2);
// 再分别去左右子树找另一个有相同值的节点
if (result !== true) result = HasSubtree(pRoot1.left,pRoot2);
if (result !== true) result = HasSubtree(pRoot1.right,pRoot2);
}
return result;
}
function doesTree1HaveTree2(root1,root2)
{
// 当root2已经遍历完,还没有返回false,说明之前遍历的值都相同
if(root2 === null) return true;
// 当root2没被遍历完而root1已经被遍历完了,说明不相同
if(root1 === null) return false;
// 当有任意一个值不相同,则返会false
if(root1.val !== root2.val) return false;
// 判断左右子树节点是否相同
return doesTree1HaveTree2(root1.left,root2.left)&&
doesTree1HaveTree2(root1.right,root2.right)
}
开始我把 if(root1 === null) 这个判断放在了最前面…找bug找了好久…
当root1遍历完的时候,要确保root2每遍历完,才能说明不相同。放在最前面的话,有可能存在root1遍历完、root2也刚好遍历完的情况。所以说,顺序很重要!
第十八题 二叉树的镜像
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
你细品,是不是就是左右子树互换?
方法一:递归
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Mirror(root)
{
// write code here
if (root === null) return false;
var tmp = root.left;
root.left = root.right;
root.right = tmp;
Mirror(root.left);
Mirror(root.right);
}
我们或许还记得递归的终极思想是数学归纳法,我们思考递归的时候一定不要去一步一步看它执行了啥,只会更绕。我们牢牢记住,思考的方式是我们首先假设子问题都已经完美处理,我只需要处理一下最终的问题即可,子问题的处理方式与最终那个处理方式一样,但是问题规模一定要以1的进制缩小。最后给一个递归出口条件即可。
对于本题,首先假设root的左右子树已经都处理好了,即左子树自身已经镜像了,右子树自身也镜像了,那么最后一步就是交换左右子树,问题解决。所以我只需要将root.left和root.right交换即可。下面进入递归,就是处理子问题。子问题的处理方式与root一样,只是要缩小问题规模,所以只需要考虑root.left是什么情况,root.right是什么情况即可。
链接:https://www.nowcoder.com/questionTerminal/564f4c26aa584921bc75623e48ca3011?f=discussion
来源:牛客网
方法二:非递归
学习自评论区~ 重点就是在判断循环的条件。很容易想到,当左子树、右子树不为空时继续下一个循环,但因为每个节点都有两个分支,而指针移动只能选一个方向,这样是无法将全部左右子树交换的。这里借一个栈存放左右子树,每一个节点都会将左右子树存起来,这样就能完全交换了~
function Mirror(root)
{
if(root === null) return root;
let stack = [root];
while(stack.length){
let x = stack.pop();
[x.left, x.right] = [x.right, x.left]; // 通过es6解构赋值交换 很简洁
if(x.left) stack.push(x.left);
if(x.right) stack.push(x.right);
}
}