Leetcode 110.平衡二叉树

题目:110.平衡二叉树

初始思路

可以根据104.二叉树的最大深度改一下,获取左右两边子树的深度,然后比较。

代码

  1. // 递归
  2. var isBalanced = function (root) {
  3. const getDepth = function (node) {
  4. // 递归终止条件
  5. if (!node) return 0
  6. let leftDepth = getDepth(node.left)
  7. // 当判定左子树不为平衡二叉树时,即可直接返回-1
  8. if (leftDepth === -1) return -1
  9. let rightDepth = getDepth(node.right)
  10. // 当判定右子树不为平衡二叉树时,即可直接返回-1
  11. if (rightDepth === -1) return -1
  12. let delta = Math.abs(leftDepth - rightDepth)
  13. if (delta > 1) {
  14. // 当前左子树右子树高度相差大于1就返回-1
  15. return -1
  16. } else {
  17. return 1 + Math.max(leftDepth, rightDepth)
  18. }
  19. }
  20. return !(getDepth(root) === -1)
  21. };
  1. // 获取当前节点的高度
  2. var getHeight = function (curNode) {
  3. let queue = [];
  4. if (curNode !== null) queue.push(curNode); // 压入当前元素
  5. let depth = 0, res = 0;
  6. while (queue.length) {
  7. let node = queue[queue.length - 1]; // 取出栈顶
  8. if (node !== null) {
  9. queue.pop();
  10. queue.push(node); // 中
  11. queue.push(null);
  12. depth++;
  13. node.right && queue.push(node.right); // 右
  14. node.left && queue.push(node.left); // 左
  15. } else {
  16. queue.pop();
  17. node = queue[queue.length - 1];
  18. queue.pop();
  19. depth--;
  20. }
  21. res = res > depth ? res : depth;
  22. }
  23. return res;
  24. }
  25. var isBalanced = function (root) {
  26. if (root === null) return true;
  27. let queue = [root];
  28. while (queue.length) {
  29. let node = queue[queue.length - 1]; // 取出栈顶
  30. queue.pop();
  31. if (Math.abs(getHeight(node.left) - getHeight(node.right)) > 1) {
  32. return false;
  33. }
  34. node.right && queue.push(node.right);
  35. node.left && queue.push(node.left);
  36. }
  37. return true;
  38. };

感想

  1. 递归还是挺好理解的。迭代也是模拟的后序遍历。

Leetcode 257.二叉树的所有路径

题目:257.二叉树的所有路径 讲解:https://www.bilibili.com/video/BV1ZG411G7Dh

初始思路

深度优先搜索

代码

  1. var binaryTreePaths = function (root) {
  2. let res = []
  3. //1. 确定递归函数 函数参数
  4. const getPath = function (node, curPath) {
  5. //2. 确定终止条件,到叶子节点就终止
  6. if (node.left == null && node.right == null) {
  7. // 如果遇到了叶子节点就把路径放到结果数组中
  8. curPath += node.val
  9. res.push(curPath)
  10. return
  11. }
  12. // 3. 确定单层递归逻辑
  13. // 如果没遇到叶子节点就把这个点加入curPath上
  14. curPath += node.val + '->'
  15. // 继续遍历左右子树
  16. if (node.left) {
  17. getPath(node.left, curPath)
  18. }
  19. if (node.right) {
  20. getPath(node.right, curPath)
  21. }
  22. }
  23. getPath(root, '')
  24. return res
  25. };
  1. var binaryTreePaths = function (root) {
  2. if (!root) return []
  3. const stack = [root], paths = [''], res = []
  4. while (stack.length) {
  5. const node = stack.pop()
  6. let path = paths.pop()
  7. if (node.left == null && node.right == null) {
  8. // 如果遇到了叶子节点就把路径放到结果数组中
  9. res.push(path + node.val)
  10. continue
  11. }
  12. // 如果没遇到叶子节点就把这个节点继续连上
  13. path += node.val + '->'
  14. if (node.right) {
  15. stack.push(node.right)
  16. paths.push(path)
  17. }
  18. if (node.left) {
  19. stack.push(node.left)
  20. paths.push(path)
  21. }
  22. }
  23. return res
  24. }

感想

  1. 过程
    image.png
  2. 这次的迭代还挺好理解的,有点像层序遍历(

Leetcode 404.左叶子之和

题目:404.左叶子之和

初始思路

感觉用层序遍历可以做出来

代码

  1. var sumOfLeftLeaves = function (root) {
  2. const nodeSum = function (node) {
  3. // 终止条件
  4. if (!node) return 0
  5. let leftValue = nodeSum(node.left) // 左
  6. let rightValue = nodeSum(node.right) // 右
  7. // 单层递归逻辑
  8. let midValue = 0 // 中
  9. if (node.left != null && node.left.left == null && node.left.right == null){
  10. // 如果这个节点左节点存在,并且左节点没有左右节点,说明是我们需要的左叶子
  11. midValue = node.left.val
  12. }
  13. let sum = midValue + leftValue + rightValue
  14. return sum
  15. }
  16. return nodeSum(root)
  17. };
  1. var sumOfLeftLeaves = function (root) {
  2. if (!root) return null
  3. let queue = []
  4. let sum = 0
  5. queue.push(root)
  6. while (queue.length) {
  7. let node = queue.shift()
  8. if (node.left != null && node.left.left == null && node.left.right == null){
  9. sum +=node.left.val
  10. }
  11. if (node.left) {
  12. queue.push(node.left)
  13. }
  14. if (node.right) {
  15. queue.push(node.right)
  16. }
  17. }
  18. return sum
  19. }

感想

  1. 左叶子的明确定义:节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点。
    image.png
  2. 判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子。
  3. 迭代就是层序遍历,只是在遍历的模板上加了判断,然后累加。