Skip to main content

10 posts tagged with "binaryTree"

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/convert-sorted-array-to-binary-search-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 {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function (nums) {
if (nums.length === 0) {
return null;
}
return createBST(nums, 0, nums.length - 1);
};

const createBST = (nums, low, high) => {
if (low > high) {
return null;
}

let mid = Math.floor((high + low) / 2);

let node = new TreeNode(nums[mid]);
node.left = createBST(nums, low, mid - 1);
node.right = createBST(nums, mid + 1, high);
return node;
};

· One min read

https://leetcode.com/problems/balanced-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 {boolean}
*/
var isBalanced = function (root) {
if (root === null) {
return true;
}
return checkDepth(root) !== false;
};

const checkDepth = (node) => {
if (node === null) {
return 0;
}

// 각각 false 혹은 왼쪽/오른쪽 자식의 최대 깊이
const left = checkDepth(node.left);
const right = checkDepth(node.right);

if (left === false || right === false || Math.abs(left - right) > 1) {
return false;
}

// 트리의 가장 깊은 길이를 return
return Math.max(left, right) + 1;
};

· 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)
);
};

· One min read

https://leetcode.com/problems/binary-tree-preorder-traversal/

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 preorderTraversal = function (root) {
let result = [];

traversalHelper(root, result);

return result;
};

const traversalHelper = (root, result) => {
if (root === null) {
return;
}
result.push(root.val);
traversalHelper(root.left, result);
traversalHelper(root.right, result);
};

· One min read

https://leetcode.com/problems/binary-tree-postorder-traversal/

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 postorderTraversal = function (root) {
let result = [];
traversalHelper(root, result);
return result;
};

const traversalHelper = (root, result) => {
if (root === null) {
return;
}
result.unshift(root.val);
traversalHelper(root.right, result);
traversalHelper(root.left, result);
};

· One min read

https://leetcode.com/problems/binary-tree-inorder-traversal/

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 inorderTraversal = function (root) {
let stack = [];
let result = [];

while (root !== null || stack.length !== 0) {
while (root !== null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
result.push(root.val);
root = root.right;
}
return result;
};