Skip to main content

5 posts tagged with "bfs"

View All Tags

· One min read

https://leetcode.com/problems/same-tree/

Solution 1

/**
* 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} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (p === null && q === null) {
return true;
}

if ((p !== null && q === null) || (p === null && q !== null)) {
return false;
}

if (p.val !== q.val) {
return false;
}

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};

· One min read

https://leetcode.com/problems/symmetric-tree/

Solution 1

/**
* 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 {boolean}
*/
var isSymmetric = function (root) {
return root === null || isMirrorValue(root.left, root.right);
};

const isMirrorValue = (left, right) => {
if (left === null && right === null) {
return true;
}
if (left === null || right === null) {
return false;
}

if (left.val === right.val) {
return (
isMirrorValue(left.right, right.left) &&
isMirrorValue(left.left, right.right)
);
} else {
return false;
}
};

· One min read

https://leetcode.com/problems/maximum-depth-of-binary-tree/

Solution 1

/**
* 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 maxDepth = function (root) {
if (root === null) {
return 0;
}

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};

· One min read

https://leetcode.com/problems/minimum-depth-of-binary-tree/

Solution 1

/**
* 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) {
if (root === null) {
return 0;
}

if (root.left === null) {
return minDepth(root.right) + 1;
}
if (root.right === null) {
return minDepth(root.left) + 1;
}

return 1 + Math.min(minDepth(root.left), minDepth(root.right));
};

· One min read

https://leetcode.com/problems/path-sum/

Solution 1

/**
* 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 === null) {
return false;
}

const calculatedValue = targetSum - root.val;
if (root.left === null && root.right === null) {
if (calculatedValue === 0) {
return true;
}
return false;
}

return (
hasPathSum(root.left, calculatedValue) ||
hasPathSum(root.right, calculatedValue)
);
};