一、最大二叉树
/**
* 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 {number[]} nums
* @return {TreeNode}
*/
var constructMaximumBinaryTree = function(nums) {
if(!nums.length) {
return null
}
if(nums.length === 1) {
return new TreeNode(nums[0])
}
let maxVal = nums[0], maxIndex = 0
for(let i = 1; i < nums.length; i++) {
if(nums[i] > maxVal) {
maxVal = nums[i]
maxIndex = i
}
}
let node = new TreeNode(maxVal)
node.left = constructMaximumBinaryTree(nums.slice(0, maxIndex))
node.right = constructMaximumBinaryTree(nums.slice(maxIndex + 1))
return node
};
二、合并二叉树
递归法
/**
* 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} root1
* @param {TreeNode} root2
* @return {TreeNode}
*/
var mergeTrees = function(root1, root2) {
if(!root1) {
return root2
}
if(!root2) {
return root1
}
root1.val += root2.val
root1.left = mergeTrees(root1.left, root2.left)
root1.right = mergeTrees(root1.right, root2.right)
return root1
};
层序遍历法
var mergeTrees = function(root1, root2) {
if(!root1) {
return root2
}
if(!root2) {
return root1
}
let queue = [root1, root2]
while(queue.length) {
let node1 = queue.shift()
let node2 = queue.shift()
node1.val += node2.val
if(node1.left && node2.left) {
queue.push(node1.left, node2.left)
}
if(node1.right && node2.right) {
queue.push(node1.right, node2.right)
}
if(!node1.left) {
node1.left = node2.left
}
if(!node1.right) {
node1.right = node2.right
}
}
return root1
};
三、二叉搜索树中的搜索
递归法
/**
* 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} val
* @return {TreeNode}
*/
var searchBST = function(root, val) {
if(!root || root.val === val) {
return root
}
if(root.val > val) {
return searchBST(root.left, val)
}
if(root.val < val) {
return searchBST(root.right, val)
}
};
迭代法
不需要栈和队列模拟,因为二叉搜索树可以确定查找方向
var searchBST = function (root, val) {
while (root) {
if (root.val > val) {
root = root.left
} else if(root.val < val){
root = root.right
} else {
return root
}
}
return root
};
四、验证二叉搜索树
递归中序遍历法
中序遍历下的二叉搜索树是递增的有序序列
/**
* 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 {boolean}
*/
var isValidBST = function(root) {
let maxVal = -Infinity
function dfs(root) {
if(!root) {
return true
}
let left = dfs(root.left)
if(maxVal < root.val) {
maxVal = root.val
} else {
return false
}
let right = dfs(root.right)
return left && right
}
return dfs(root)
};
迭代法,中序遍历
var isValidBST = function(root) {
let stack = []
let cur = root
let pre
while(stack.length || cur) {
if(cur) {
stack.push(cur)
cur = cur.left
} else {
let node = stack.pop()
if(pre && pre.val >= node.val) {
return false
}
pre = node
cur = node.right
}
}
return true
};