El ancestro común y la ruta de dos tipos se escriben juntos
235. Ancestro común más cercano de los árboles de búsqueda binarios
235. Ancestro común más cercano de los árboles de búsqueda binarios
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
if (!root) return null;
const dfs = (node) => {
if (node == null) return null;
if (node.val == p.val || node.val == q.val) {
return node;
}
const left = dfs(node.left);
const right = dfs(node.right);
if (left && right) {
return node;
}
if (left == null) {
return right;
}
return left;
}
return dfs(root);
};
复制代码
236. El ancestro común más cercano de un árbol binario
236. El ancestro común más cercano de un árbol binario
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
if(root == null) return null;
const dfs = (node) => {
if (node == null) return null;
if (node == p || node == q) {
return node;
}
let left = dfs(node.left);
let right = dfs(node.right);
if (left && right) {
return node;
}
if(left == null) {
return right;
}
return left;
};
return dfs(root);
};
复制代码
129. Encuentra la suma de los números desde el nodo raíz hasta el nodo hoja
129. Encuentra la suma de los números desde el nodo raíz hasta el nodo hoja
/**
* 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 sumNumbers = function(root) {
if (root == null) return 0;
let path = [];
const DFS = (node, sum) => {
if (node == null) return;
if (node.left == null && node.right == null) {
sum = sum * 10 + node.val;
path.push(sum);
}
sum = sum * 10 + node.val;
node.left && DFS(node.left, sum);
node.right && DFS(node.right, sum);
}
DFS(root, 0);
return path.reduce((prev, next) => {
return prev+next;
}, 0);
};
复制代码
257. Todos los caminos en un árbol binario
257. Todos los caminos en un árbol binario
/**
* 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 {string[]}
*/
var binaryTreePaths = function(root) {
if (root == null) return [];
let res = [];
const DFS = (node, arr) => {
if (!node) return;
arr.push(node.val);
if (node.left == null && node.right == null) {
let str = arr.join('->');
res.push(str);
}
node.left && DFS(node.left, arr);
node.right && DFS(node.right, arr);
arr.pop();
}
DFS(root, []);
return res;
};
复制代码