一、二叉树的下一个结点
题目描述: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
分析二叉树的下一个结点: (1)、二叉树为空,则返回空; (2)、如果结点右孩子存在,则设置一个指针从该结点的右孩子出发,一直沿着左孩子的指针找到的叶子结点即为下一个结点。 (3)、结点不是根结点, 如果该结点是其父结点左孩子,则返回父结点; 否则继续向上遍历其父结点的父结点,重复之前的判断,返回结果。
/*function TreeLinkNode(x){##
this.val = x;
this.left = null;
this.right = null;
this.next = null;
}*/
function GetNext(pNode)
{
// write code here
if (pNode == null) return null;
if (pNode.right != null) {
var node = pNode.right;
while(node.left != null) node = node.left;
return node;
}
while(pNode.next != null) {
if (pNode.next.left == pNode) return pNode.next;
pNode = pNode.next;
}
return null;
}
二、把二叉树打印多行
题目描述: 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
树的层次遍历,用队列 (BFS广度优先搜索)
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Print(root)
{
// write code here
if (root == null) return [];
var queue = [], res = [];
queue.push(root);
while(queue.length) {
var arr = [];
var len = queue.length;
for(var i = 0; i < len; i++) {
var temp = queue.shift();
arr.push(temp.val);
if (temp.left) {
queue.push(temp.left);
}
if (temp.right) {
queue.push(temp.right);
}
}
res.push(arr);
}
return res;
}
三、按之字形顺序打印二叉树
题目描述: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
与上一题类似,用队列,进行树的层次遍历的同时,用一个变量去判断偶数层,数组反转,从右到左。
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Print(root)
{
// write code here
if (!root) return [];
var queue = [], res = [];
var flag = true;
queue.push(root);
while(queue.length) {
var arr = [];
let len = queue.length;
for(var i = 0; i < len; i++) {
var temp = queue.shift();
arr.push(temp.val);
if (temp.left) {
queue.push(temp.left);
}
if (temp.right) {
queue.push(temp.right);
}
}
if (!flag) {
arr.reverse();
}
flag = !flag;
res.push(arr);
}
return res;
}
四、二叉搜索树的第k个结点
题目描述: 给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。
中序遍历的结果就是有序序列,第k个元素就是arr[k-1]存储的节点指针
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function KthNode(pRoot, k){
if(pRoot == null || k <= 0) return null;
var arr = [];
inOrder(pRoot, arr);
return arr[k - 1];
}
function inOrder(pRoot, arr) {
if(pRoot != null) {
if(pRoot.left) inOrder(pRoot.left, arr);
arr.push(pRoot);
if(pRoot.right) inOrder(pRoot.right, arr);
}
}
五、对称的二叉树
题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
非递归: DFS =》 使用栈来保存成对的结点 (1)、若都为空,则继续; (2)、若一个为空,则返回false; (3)、若不为空,则比较当前值,值不想等就返回false。 每次出栈和入栈顺序都是成对的。
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function isSymmetrical(pRoot)
{
// write code here
if (pRoot == null) return true;
var stack = [];
stack.push(pRoot.left);
stack.push(pRoot.right);
while(stack.length > 0) {
var right = stack.pop();
var left = stack.pop(); // 成对的取出
if (left == null && right == null) continue;
if (left == null || right == null) return false;
if (left.val != right.val) return false;
stack.push(left.left);
stack.push(right.right);
//成对的插入
stack.push(left.right);
stack.push(right.left);
}
return true;
}

本文深入探讨了二叉树的多种算法实现,包括中序遍历找下一个节点、层次打印、之字形打印、查找第k个节点及判断是否对称。提供了详细的代码解析,适合算法初学者和进阶者。
7753

被折叠的 条评论
为什么被折叠?



