1. 二叉树的前中后遍历
- 中序遍历
var inorderTraversal = function (root, array = []) {
if(root){
inorderTraversal(root.left, array);
array.push(root);
inorderTraversal(root.right, array);
}
return array;
}
- 前序遍历
var preorderTraversal = function (root, array = []) {
if (root) {
array.push(root.val);
preorderTraversal(root.left, array);
preorderTraversal(root.right, array);
}
return array;
};
- 后序遍历
var postorderTraversal = function (root, array = []) {
if (root) {
postorderTraversal(root.left, array);
postorderTraversal(root.right, array);
array.push(root.val);
}
return array;
};
2. 二叉树的镜像
操作给定的二叉树,将其变换为源二叉树的镜像。
比如: 源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {void}
*/
var mirror = function(root) {
if (!root) return ;
mirror(root.left);
mirror(root.right);
let tmp = root.left;
root.left = root.right;
root.right = tmp;
};
递归写法即可
3. 二叉树的深度
输入一棵二叉树的根结点,求该树的深度。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var treeDepth= function(root) {
if(!root) return 0;
return Math.max(treeDepth(root.left), treeDepth(root.right)) + 1;
};
4. 平衡二叉树
输入一棵二叉树的根结点,判断该树是不是平衡二叉树。
如果某二叉树中任意结点的左右子树的深度相差不超过 1,那么它就是一棵平衡二叉树。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
let ans = true;
dfs(root);
function dfs(root) {
if(!root) return 0;
let left = dfs(root.left), right = dfs(root.right);
if(Math.abs(left-right) > 1) ans = false;
return Math.max(left, right) + 1;
}
return ans;
};
5. 打印二叉树多行
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var printFromTopToBottom = function(root) {
if(!root) return [];
const q = [root];
const res = [];
let level = 0;
while(q.length) {
res[level] = [];
let size = q.length;
for(let i = 0; i < size; i ++) {
const t = q.shift();
res[level].push(t.val);
if(t.left) q.push(t.left);
if(t.right) q.push(t.right);
}
level ++;
}
return res;
};
6. 对称的二叉树
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function isSymmetrical(pRoot)
{
// write code here
function dfs(p1, p2){
if(!p1 && !p2) return true;
if(!p1 || !p2) return false;
if(p1.val !== p2.val) return false;
return dfs(p1.left,p2.right) && dfs(p1.right, p2.left);
}
if(!pRoot) return true;
else return dfs(pRoot.left, pRoot.right);
}
7. 二叉树的下一个节点
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = this.father = null;
* }
*/
/**
* @param {TreeNode} p
* @return {TreeNode}
*/
var inorderSuccessor = function(p) {
if(!p) return null;
// case 1 有右子树
if(p.right){
p = p.right;
while(p.left){
p = p.left;
}
return p;
}
// 2. 无右子树
while(p.father){
if(p.father.left === p){
return p.father;
}
p = p.father;
}
return null;
};
8. 从上到下打印二叉树
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var printFromTopToBottom = function(root){
const res = []
if(!root) return res
const q = [root]
while(q.length) {
const node = q.shift()
res.push(node.val)
node.left && q.push(node.left)
node.right && q.push(node.right)
}
return res
}
9. 二叉树中和为某一值的路径
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} sum
* @return {number[][]}
*/
var findPath = function(root, sum) {
const path = []
const ans = []
function dfs(root, sum) {
if(!root) return
path.push(root.val)
sum -= root.val
if(!root.left && !root.right && !sum) ans.push([...path])
dfs(root.left,sum)
dfs(root.right,sum)
path.pop()
}
dfs(root,sum)
return ans
};
10. 重建二叉树
/**
* 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 || !inorder.length) return null;
const rootVal = preorder[0];
const node = new TreeNode(rootVal);
let i = 0;
for(; i < inorder.length; i++){
if(inorder[i] === rootVal) break;
}
node.left = buildTree(preorder.slice(1, i + 1), inorder.slice(0, i)); //前序的前i个数
node.right = buildTree(preorder.slice(i + 1), inorder.slice(i+1));
return node;
};
11 按之字形顺序打印二叉树
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Print(pRoot)
{
// write code here
const res = [];
if(!pRoot) return res;
const q = [pRoot];
let level = 0;
while(q.length) {
res[level] = []
let size = q.length;
for(let i = 0; i < size; i++){
const t = q.shift();
if(level % 2 === 0) res[level].push(t.val);
else res[level].unshift(t.val)
if(t.left) q.push(t.left)
if(t.right) q.push(t.right)
}
level ++;
}
return res;
}
12 二叉搜索树的后续遍历序列
/**
* @param {number[]} sequence
* @return {bool}
*/
var verifySequenceOfBST = function(sequence) {
function dfs(l,r) {
if (l >= r) return true
let root = sequence[r]
let k = l
while(k < r && sequence[k] < root) k++
for(let i = k; i < r; i ++) {
if(sequence[i] < root){
return false
}
}
return dfs(l,k-1) && dfs(k,r-1)
}
return dfs(0, sequence.length-1)
};
13 树的子结构
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} pRoot1
* @param {TreeNode} pRoot2
* @return {boolean}
*/
var hasSubtree = function(pRoot1, pRoot2) {
function isPart (p1, p2) {
if (!p2) return true;
if (!p1 || p1.val != p2.val) return false;
return isPart(p1.left,p2.left) && isPart(p1.right,p2.right);
}
if (!pRoot1 || !pRoot2) return false;
if (isPart(pRoot1, pRoot2)) return true;
return hasSubtree(pRoot1.left, pRoot2) || hasSubtree(pRoot1.right, pRoot2);
};
14 序列化二叉树
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Serialize(pRoot)
{
// write code here
return JSON.stringify(pRoot)
}
function Deserialize(s)
{
// write code here
return JSON.parse(s)
}
15 二叉搜索树的第k个元素
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function KthNode(pRoot, k)
{
// write code here
if(k < 0 || !pRoot) return null;
let ans = [];
function dfs(root){
if(root){
dfs(root.left);
ans.push(root);
dfs(root.right);
}
}
dfs(pRoot);
return ans[k-1];
}
LeetCode111 二叉树最小深度
/**
* 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) {
return dfs(root);
function dfs(root){
if(!root) return 0;
if(root.left && !root.right) return dfs(root.left) + 1;
if(!root.left && root.right) return dfs(root.right) + 1;
return Math.min(dfs(root.left), dfs(root.right)) + 1;
}
};
LeetCode97 二叉树的中序遍历
递归版本
/**
* 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 inorderTraversal = function(root) {
const res = [];
dfs(root);
return res;
function dfs(root){
if(root){
dfs(root.left);
res.push(root.val);
dfs(root.right);
}
};
};
非递归版本
const inorderTraversal = (root) => {
const res = [];
const stack = [];
while (root) { // 能压入栈的左子节点都压进来
stack.push(root);
root = root.left;
}
while (stack.length) {
let node = stack.pop(); // 栈顶的节点出栈
res.push(node.val); // 在压入右子树之前,处理它的数值部分(因为中序遍历)
node = node.right; // 获取它的右子树
while (node) { // 右子树存在,执行while循环
stack.push(node); // 压入当前root
node = node.left; // 不断压入左子节点
}
}
return res;
};
LeetCode112 路径总和
/**
* 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} targetSum
* @return {boolean}
*/
var hasPathSum = function(root, targetSum) {
if(!root) return false;
let res = false;
const dfs = (n, s) => {
// console.log(n.val, s);
if(!n.left && !n.right && s === targetSum) res = true;
if(n.left) dfs(n.left, s + n.left.val);
if(n.right) dfs(n.right, s + n.right.val);
}
dfs(root, root.val);
return res;
};
很有意思