代码随想录算法训练营Day17 二叉树part03|110.平衡二叉树、257.二叉树所有路径、404.左叶子之和
今日内容
- 110.平衡二叉树
- 257.二叉树所有路径
- 404.左叶子之和
110.平衡二叉树 (优先掌握递归)
平衡二叉树:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1
二叉树节点的高度:距离叶子节点的高度
二叉树节点的深度:距离根节点的深度(指从根节点到该节点的最长简单路径边的条数)

求高度:后序遍历(左右中)
求深度:前序遍历(中左右)
后序:
根据左右子树的情况返回给父节点,一层层向上
前序:
一直向下遍历,往下统计
做题思路
1. 求高度
递归三部曲:
① 第一步:确定递归函数的参数和返回值
参数:传入当前节点
返回值:以当前传入节点为根节点的树的高度,如果不是平衡二叉树,则返回 -1
const TreeHigh = function(node){}
let leftHeight = TreeHigh(node.left)
let rightHeight = TreeHigh(node.right)
if(leftHeight === -1 || rightHeight === -1) return -1
② 第二步:确定终止条件
遇到空姐点则终止,返回0,表示当前节点为根节点的树高度为0
if(node === null) return 0
③ 第三步:明确单层递归的逻辑
let leftHeight = TreeHigh(node.left)
let rightHeight = TreeHigh(node.right)
let result = 0;
if(abs(leftHeight,righeHeiht) >1 ) return -1;
else{
result = 1+max(leftHeight,rightHeight);// 父节点的高度,等于左右子树中高度最大值+1(1为本身高度)
}
return result;
左右子树的高度差>1,则向上返回-1
1 /* 2 * @lc app=leetcode.cn id=110 lang=javascript 3 * 4 * [110] 平衡二叉树 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {TreeNode} root 18 * @return {boolean} 19 */ 20 var isBalanced = function(root) { 21 // 1. 确定参数和返回值 22 const TreeHigh = function(node) { 23 // 2.确定终止条件 24 if (node === null) return 0; 25 // 3. 确定单层递归逻辑 26 let leftHeigh = TreeHigh(node.left); 27 let rightHeigh = TreeHigh(node.right); 28 // 如果leftHeigh 和 rightHeigh 中有-1,则返回-1 29 if (leftHeigh === -1 || rightHeigh === -1) return -1; 30 // 如果左右子树高度差>1,返回-1 31 if (Math.abs(leftHeigh - rightHeigh) > 1) return -1; 32 // 正常情况: 33 34 return 1 + Math.max(leftHeigh, rightHeigh); 35 } 36 return !(TreeHigh(root) === -1); 37 };
257. 二叉树的所有路径 (优先掌握递归)
回溯
不是太懂这段代码
1 /* 2 * @lc app=leetcode.cn id=257 lang=javascript 3 * 4 * [257] 二叉树的所有路径 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {TreeNode} root 18 * @return {string[]} 19 */ 20 var binaryTreePaths = function(root) { 21 // 递归遍历+递归三部曲 22 let res = []; 23 // 1. 确定递归函数,函数参数 24 const getPath = function(node, curPath) { 25 //2. 确定终止条件,到叶子节点就终止 26 if (!node.left && !node.right) { 27 curPath += node.val; 28 res.push(curPath); 29 return; 30 } 31 // 3.确定单层递归逻辑 32 curPath += node.val + '->'; 33 node.left && getPath(node.left, curPath); 34 node.right && getPath(node.right, curPath); 35 } 36 getPath(root, '') 37 return res; 38 };
404.左叶子之和 (优先掌握递归)
注意:节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点
1 if (node.left !== null && node.left.left == NULL && node.left.right == null) { // 左叶子节点处理逻辑 }
不是严格意义上的递归写法,定义了遍历result,直接在遍历到左叶子时进行相加
1 /* 2 * @lc app=leetcode.cn id=404 lang=javascript 3 * 4 * [404] 左叶子之和 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {TreeNode} root 18 * @return {number} 19 */ 20 var sumOfLeftLeaves = function(root) { 21 //1. 确定函数参数,与返回值 22 let result = 0; 23 if (root == null) return 0; 24 const sumLeftLeave = function(node) { 25 //2. 确定终止条件 26 if (node == null) return; 27 28 //3. 单层递归逻辑 29 if (node.left !== null && !node.left.right && !node.left.left) { 30 result += node.left.val; 31 } 32 node.left && sumLeftLeave(node.left); 33 node.right && sumLeftLeave(node.right); 34 } 35 sumLeftLeave(root); 36 return result; 37 };
严格一点的写法(参考答案)
1 var sumOfLeftLeaves = function(root) { 2 //采用后序遍历 递归遍历 3 // 1. 确定递归函数参数 4 const nodesSum = function(node) { 5 // 2. 确定终止条件 6 if(node === null) { 7 return 0; 8 } 9 let leftValue = nodesSum(node.left); 10 let rightValue = nodesSum(node.right); 11 // 3. 单层递归逻辑 12 let midValue = 0; 13 if(node.left && node.left.left === null && node.left.right === null) { 14 midValue = node.left.val; 15 } 16 let sum = midValue + leftValue + rightValue; 17 return sum; 18 } 19 return nodesSum(root); 20 };
收获
1. 平衡二叉树中,高度与深度的定义
2. 递归与回溯(回溯还有点不是太明白)

浙公网安备 33010602011771号