LeetCode与《代码随想录》二叉树篇:做题笔记与总结-JavaScript版
Posted karshey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode与《代码随想录》二叉树篇:做题笔记与总结-JavaScript版相关的知识,希望对你有一定的参考价值。
文章目录
- 代码随想录
- 144. 二叉树的前序遍历
- 94. 二叉树的中序遍历
- 145. 二叉树的后序遍历
- 102.二叉树的层序遍历
- 226.翻转二叉树
- 101. 对称二叉树
- 104.二叉树的最大深度
- 111.二叉树的最小深度
- 222.完全二叉树的节点个数
- 110.平衡二叉树
- 257. 二叉树的所有路径
- 404.左叶子之和
- 513.找树左下角的值
- 112. 路径总和
- 106.从中序与后序遍历序列构造二叉树
- 105. 从前序与中序遍历序列构造二叉树
- 654.最大二叉树
- 617.合并二叉树
- 700.二叉搜索树中的搜索
- 98.验证二叉搜索树
- 530.二叉搜索树的最小绝对差
- 501.二叉搜索树中的众数
- 236. 二叉树的最近公共祖先
- 235. 二叉搜索树的最近公共祖先
- 701.二叉搜索树中的插入操作
- 450.删除二叉搜索树中的节点
- 669. 修剪二叉搜索树
- 108.将有序数组转换为二叉搜索树
- 538.把二叉搜索树转换为累加树
- 相关题目
代码随想录
前、中、后指的都是根的位置。
144. 二叉树的前序遍历
https://leetcode.cn/problems/binary-tree-preorder-traversal/
var preorderTraversal = function (root)
// 前序遍历:中 左 右
let ans = []
const dfs = function (root)
if (root === null) return;
ans.push(root.val);
dfs(root.left);
dfs(root.right);
dfs(root);
return ans;
;
94. 二叉树的中序遍历
https://leetcode.cn/problems/binary-tree-inorder-traversal/
var inorderTraversal = function (root)
let ans = []
const dfs = function (root)
if (root === null) return;
// 中序:左中右
dfs(root.left);
ans.push(root.val);
dfs(root.right);
dfs(root);
return ans;
;
145. 二叉树的后序遍历
https://leetcode.cn/problems/binary-tree-postorder-traversal/
var postorderTraversal = function (root)
let ans = []
const dfs = function (root)
if (root === null) return;
// 后序:左右中
dfs(root.left);
dfs(root.right);
ans.push(root.val);
dfs(root);
return ans;
;
102.二叉树的层序遍历
https://leetcode.cn/problems/binary-tree-level-order-traversal/
var levelOrder = function (root)
let ans = [], queue = []
if (root === null) return ans;
queue.push(root);
while (queue.length)
let len = queue.length;
let anss = [];
for (let i = 0; i < len; i++)
let node = queue.shift();
anss.push(node.val);
node.left && queue.push(node.left);
node.right && queue.push(node.right);
ans.push(anss);
return ans;
;
226.翻转二叉树
https://leetcode.cn/problems/invert-binary-tree/
var invertTree = function (root)
const dfs = function (root)
if (root === null) return;
dfs(root.left);
dfs(root.right);
[root.left, root.right] = [root.right, root.left];
dfs(root);
return root;
;
101. 对称二叉树
https://leetcode.cn/problems/symmetric-tree/
var isSymmetric = function (root)
let ans = true;
if (root === null) return ans;
if (root.left && root.right === null) return false;
if (root.right && root.left === null) return false;
const dfs = function (l, r)
if (!l && !r) return;
if (l && !r)
ans = false; return;
if (!l && r)
ans = false; return;
if (l.val !== r.val)
ans = false;
return;
dfs(l.left, r.right);
dfs(l.right, r.left);
dfs(root.left, root.right);
return ans;
;
104.二叉树的最大深度
https://leetcode.cn/problems/maximum-depth-of-binary-tree/
var maxDepth = function (root)
let ans = 0;
if (!root) return ans;
let q = [];
q.push(root);
while (q.length)
ans++;
let len = q.length;
for (let i = 0; i < len; i++)
let node = q.shift();
node.left && q.push(node.left);
node.right && q.push(node.right);
return ans;
;
111.二叉树的最小深度
https://leetcode.cn/problems/minimum-depth-of-binary-tree/
var minDepth = function (root)
// 若一个节点没有叶子节点,则结束
let ans = 0;
if (!root) return ans;
let q = [];
q.push(root);
while (q.length)
ans++;
let len = q.length;
for (let i = 0; i < len; i++)
let node = q.shift();
if (!node.left && !node.right) return ans;
node.left && q.push(node.left);
node.right && q.push(node.right);
;
222.完全二叉树的节点个数
https://leetcode.cn/problems/count-complete-tree-nodes/
注意:要利用完全二叉树的性质。
var countNodes = function (root)
if (!root) return 0;
let l = root.left, r = root.right;
let ll = 0, rr = 0;
while (l)
ll++; l = l.left;
while (r)
rr++; r = r.right;
if (ll === rr)
return Math.pow(2, ll + 1) - 1;
else
return countNodes(root.left) + countNodes(root.right) + 1;
;
110.平衡二叉树
https://leetcode.cn/problems/balanced-binary-tree/
var isBalanced = function (root)
var getHeight = function (root)
if (!root) return 0;
let l = getHeight(root.left);
if (l === -1) return -1;
let r = getHeight(root.right);
if (r === -1) return -1;
if (Math.abs(l - r) > 1) return -1;
else return Math.max(l, r) + 1;
let ans = getHeight(root);
if (ans === -1) return false;
else return true;
;
257. 二叉树的所有路径
https://leetcode.cn/problems/binary-tree-paths/
var binaryTreePaths = function (root)
let ans = [], anss = [];
var dfs = function (root)
anss.push(root.val);
root.left && dfs(root.left);
root.right && dfs(root.right);
if (!root.left && !root.right)
ans.push(anss.join('->'));
anss.pop();
dfs(root);
return ans;
;
404.左叶子之和
https://leetcode.cn/problems/sum-of-left-leaves/
var sumOfLeftLeaves = function (root)
let ans = 0;
var dfs = function (root, flag)
if (!root) return;
root.left && dfs(root.left, 1);
root.right && dfs(root.right, 0);
if (flag === 1 && !root.left && !root.right)
ans += root.val;
dfs(root);
return ans;
;
513.找树左下角的值
https://leetcode.cn/problems/find-bottom-left-tree-value/
var findBottomLeftValue = function (root)
// 记录bfs每一层的第一个
let ans = root.val, q = [];
q.push(root);
while (q.length)
let len = q.length;
for (let i = 0; i < len; i++)
let node = q.shift();
if (!i) ans = node.val;
node.left && q.push(node.left);
node.right && q.push(node.right);
return ans;
;
112. 路径总和
https://leetcode.cn/problems/path-sum/
var hasPathSum = function (root, targetSum)
let ans = 0, flag = false;
var dfs = function (root)
if (root.left)
ans += root.left.val;
dfs(root.left);
ans -= root.left.val;
if (root.right)
ans += root.right.val;
dfs(root.right);
ans -= root.right.val;
if (!root.left && !root.right)
if (ans === targetSum) flag = true;
if (root)
ans += root.val;
dfs(root);
return flag;
;
106.从中序与后序遍历序列构造二叉树
https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
var buildTree = function (inorder, postorder)
if (!inorder.length) return null;
let rootVal = postorder.pop();
let rootIndex = inorder.indexOf(rootVal);
let node = new TreeNode(rootVal);
node.left = buildTree(inorder.slice(0, rootIndex), postorder.slice(0, rootIndex));
node.right = buildTree(inorder.slice(rootIndex + 1), postorder.slice(rootIndex));
return node;
;
105. 从前序与中序遍历序列构造二叉树
https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
var buildTree = function (preorder, inorder)
if (!preorder.length) return null;
let rootVal = preorder.shift();
let rootIndex = inorder.indexOf(rootVal);
let node = new TreeNode(rootVal);
node.left = buildTree(preorder.slice(0, rootIndex), inorder.slice(0, rootIndex));
node.right = buildTree(preorder.slice(rootIndex), inorder.slice(rootIndex + 1));
return node;
;
654.最大二叉树
https://leetcode.cn/problems/maximum-binary-tree/
var constructMaximumBinaryTree = function (nums)
// 数组的左右边界
var dfs = function (l, r)
if (l > r) return null;
let maxVal = -1, maxIndex = -1;
for (let i = l; i <= r; i++)
if (nums[i] > maxVal)
maxVal = nums[i];
maxIndex = i;
const node = new TreeNode(maxVal);
node.left = dfs(l, maxIndex - 1);
node.right 以上是关于LeetCode与《代码随想录》二叉树篇:做题笔记与总结-JavaScript版的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode与《代码随想录》链表篇:做题笔记与总结-JavaScript版
LeetCode与《代码随想录》数组篇:做题笔记与总结-Java版
LeetCode与《代码随想录》双指针篇:做题笔记与总结-JavaScript版
LeetCode与《代码随想录》字符串篇:做题笔记与总结-JavaScript版