二叉树的最大深度
/**
* 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 maxDepth = function(root) {
return root === null?0:Math.max(maxDepth(root.left),maxDepth(root.right))+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 {boolean}
*/
var isSymmetric = function(root) {
if(root===null) return true
return isEqueal(root.left,root.right)
};
var isEqueal = function(left,right){
if(left === null&&right===null) return true
if(left === null&& right!==null) return false
if(left !== null&& right === null) return false
if(left.val === right.val&&isEqueal(left.left,right.right)&&isEqueal(left.right,right.left)) return true
return false
}
验证二叉搜索树
题目链接
思路
中序遍历后然后判断
/**
* 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) {
if(root == null && root.left ==null &&root.right == null){
return true
}
var num = []
bianli(root,num)
for(let i = 0;i<num.length-1;i++){
if(num[i]>=num[i+1]){
return false
}
}
debugger
return true
};
var bianli = function(root,num){
if(root != null){
bianli(root.left,num)
num.push(root.val)
bianli(root.right,num)
}
}
二叉树的层序遍历
题目链接
思路
层序遍历思想就是使用一个队列,然后将队列的节点遍历,遍历完后将节点的左节点和右节点插入队列,直到队列为空
/**
* 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 levelOrder = function(root) {
const queue = []//设置队列
const result = []//设置返回的结果
if(root == null){
return result
}
queue.push(root)
while(queue.length!=0){
result.push([])
let currentLength = queue.length//获取当前的长度,一定要这样设置,不然每次添加的时候长度都会增加
for(let i = 0;i<currentLength;i++){
let out = queue.shift()
result[result.length-1].push(out.val)
if(out.left) queue.push(out.left)
if(out.right) queue.push(out.right)
}
}
return result
};
二叉搜索树和双向链表
题目链接
思路
中序遍历排序后再转为双向链表
/**
* // Definition for a Node.
* function Node(val,left,right) {
* this.val = val;
* this.left = left;
* this.right = right;
* };
*/
/**
* @param {Node} root
* @return {Node}
*/
var treeToDoublyList = function(root) {
if(root == null) return null
let nums = []
midTraverse(root,nums)
for(let i =0 ;i<nums.length-1;i++){//只遍历到倒数第2个,所以倒数第一个有前驱,没后继
nums[i].right = nums[i+1]
nums[i+1].left = nums[i]
}
nums[nums.length-1].right = nums[0]//为倒数第一个设置后继
nums[nums.length-1].left = nums[nums.length-2]//为倒数第一个设置后继
nums[0].left = nums[nums.length-1]//为第一个设置前驱
return nums[0]
};
var midTraverse = function(root,nums){
if(root == null) return null
midTraverse(root.left,nums)
nums.push(root)
midTraverse(root.right,nums)
}