Js版本 二叉树整理(更新ing)
105. 从前序与中序遍历序列构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function(preorder, inorder) {
if(!preorder.length) return null
let root = new TreeNode(preorder[0]); // 得到树的根节点
let index = inorder.indexOf(root.val); //
//获取前序遍历的左右子树
let preOrderLeftTree = preorder.slice(1,index+1);
let preOrderRightTree = preorder.slice(index+1,preorder.length);
//获取中序遍历的左右子树
let inOrderLeftTree = inorder.slice(0,index);
let inOrderRightTree = inorder.slice(index+1,inorder.length);
root.left = buildTree(preOrderLeftTree,inOrderLeftTree);
root.right = buildTree(preOrderRightTree,inOrderRightTree);
return root;
}
106. 从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} inorder
* @param {number[]} postorder
* @return {TreeNode}
*/
var buildTree = function(inorder, postorder) {
if(!inorder.length)return null;
let root = new TreeNode(postorder[postorder.length-1]);
let index = inorder.indexOf(root.val);
let leftInorderTree = inorder.slice(0,index);
let rightInorderTree = inorder.slice(index+1);
let leftPostorderTree = postorder.slice(0,index);
let righPostorderTree = postorder.slice(index,postorder.length-1);
root.left = buildTree(leftInorderTree,leftPostorderTree)
root.right = buildTree(rightInorderTree,righPostorderTree)
return root
};
654. 最大二叉树
给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
二叉树的根是数组中的最大元素。
左子树是通过数组中最大值左边部分构造出的最大二叉树。
右子树是通过数组中最大值右边部分构造出的最大二叉树。
通过给定的数组构建最大二叉树,并且输出这个树的根节点。
示例 :
输入:[3,2,1,6,0,5]
输出:返回下面这棵树的根节点:
6
/ \
3 5
\ /
2 0
\
1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return Build(nums,0,nums.length-1);
}
public static TreeNode Build (int[] nums,int low,int hi){
if(low>hi){
return null;
}
int Max_val = -1;
int index = -1;
for(int i = low;i<=hi;i++){
if(nums[i]>Max_val){
Max_val = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(Max_val);
root.left = Build(nums,low, index-1);
root.right = Build(nums,index+1,hi);
return root;
}
}
230. 二叉搜索树中第K小的元素
给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素。
说明:
你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数。
示例 1:
输入: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
输出: 1
示例 2:
输入: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
输出: 3
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
var kthSmallest = function(root, k) {
if(root==null)return null;
let arr = [];
var kthSmallestArr =function(root) {
if(!root) return;
kthSmallestArr(root.left);
arr.push(root.val);
kthSmallestArr(root.right);
}
kthSmallestArr(root);
return arr[k-1];
};
538/1038. 把二叉搜索树转换为累加树
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
提醒一下,二叉搜索树满足下列约束条件:
节点的左子树仅包含键 小于 节点键的节点。
节点的右子树仅包含键 大于 节点键的节点。
左右子树也必须是二叉搜索树。
注意:该题目与 538: https://leetcode-cn.com/problems/convert-bst-to-greater-tree/ 相同
示例 1:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var bstToGst = function(root) {
if(root==null)return null;
var sum = 0;
var helper = function(root){
if(root==null)return null;
helper(root.right);
sum += root.val;
root.val = sum;
helper(root.left);
}
helper(root);
return root;
}
98. 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
示例 1:
输入:
2
/ \
1 3
输出: true
示例 2:
输入:
5
/ \
1 4
/ \
3 6
输出: false
解释: 输入为: [5,1,4,null,null,3,6]。
根节点的值为 5 ,但是其右子节点值为 4 。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST(root,null,null);
}
boolean isValidBST(TreeNode root, TreeNode min,TreeNode max){
if(root==null)return true;
if(min!= null&&root.val<=min.val)return false;
if(max!= null&&root.val>=max.val)return false;
return isValidBST(root.left,min,root)&&isValidBST(root.right,root,max);
}
}
111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:2
示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
方法一:DFS
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var minDepth = function(root) {
if(root==null)return 0;
var depth =Number.MAX_SAFE_INTEGER;
if(root.left==null&&root.right==null){
return 1;
}
if(root.left!=null){
depth = Math.min(minDepth(root.left),depth)
}
if(root.right!=null){
depth = Math.min(minDepth(root.right),depth)
}
return depth+1;
};
方法二:BFS
const minDepth = (root) => {
if (root == null) return 0;
const queue = []; // 根节点入列
let depth = 1; // 当前层的深度
queue.push(root);
while (queue.length) { // 直到清空队列
const levelSize = queue.length; // 当前层的节点个数
for (let i = 0; i < levelSize; i++) { // 遍历 逐个出列
const cur = queue.shift(); // 出列
if (cur.left == null && cur.right == null) { // 如果没有孩子,直接返回所在层数
return depth;
}
if (cur.left) queue.push(cur.left); // 有孩子,让孩子入列
if (cur.right) queue.push(cur.right);
}
depth++; // 肯定有下一层,如果没有早就return了
}
return depth;
};
剑指 Offer 33.二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。
参考以下这颗二叉搜索树:
5
/ \
2 6
/ \
1 3
示例 1:
输入: [1,6,3,2,5]
输出: false
示例 2:
输入: [1,3,2,6,5]
输出: true
var VerifySquenceOfBST = function(sequence) {
if ( sequence==null || sequence.length==0 ) return false;
return VerifyHelper(sequence,0,sequence.length-1);
};
function VerifyHelper(postOrder,left,right){
if(left>=right){
return true
}
var mid = left;
var root = postOrder[right];
while(postOrder[mid] < root){
mid ++;
}
var temp = mid;
while(temp<right){
if (postOrder[temp++] < root)
return false;
}
return VerifyHelper(postOrder,left,mid-1)&& VerifyHelper(postOrder,mid,right-1)
}
101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
function isSymmetric(root)
{
if(root==null){
return false;
}
return isSame(root,root);
}
function isSame(r1, r2){
if(r1==null && r2==null){
return true;
}
if(r1==null || r2==null){
return false;
}
if (r1.val != r2.val) {
return false;
} else {
return isSame(r1.left, r2.right) && isSame(r1.right, r2.left);
}
}

浙公网安备 33010602011771号