117. Populating Next Right Pointers in Each Node II
Description
Input: root = [1,2,3,4,5,null,7] Output: [1,#,2,3,#,4,5,7,#] Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with ‘#’ signifying the end of each level.
Solution
/** * // Definition for a Node. * function Node(val, left, right, next) { * this.val = val === undefined ? null : val; * this.left = left === undefined ? null : left; * this.right = right === undefined ? null : right; * this.next = next === undefined ? null : next; * }; */ /** * @param {Node} root * @return {Node} */ var connect = function(root) { if(root === null) returnnull; let stack = [root]; while(stack.length > 0) { let n = stack.length; for(let i = 0; i < n; i++) { stack[0].next = i === n - 1 ? null : stack[1]; if(stack[0].left !== null) stack.push(stack[0].left); if(stack[0].right !== null) stack.push(stack[0].right); stack.shift(); } } return root; };
105. Construct Binary Tree from Preorder and Inorder Traversal
Description
Given preorder and inorder traversal of a tree, construct the binary tree.
Solution
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {number[]} preorder * @param {number[]} inorder * @return {TreeNode} */ var buildTree = function(preorder, inorder) { if(preorder.length === 0 || inorder.length === 0) returnnull; let root = new TreeNode(preorder.shift()); let index = inorder.indexOf(root.val); root.left = buildTree(preorder, inorder.slice(0, index)); root.right = buildTree(preorder, inorder.slice(index + 1)); return root; };
106. Construct Binary Tree from Inorder and Postorder Traversal
Description
Given inorder and postorder traversal of a tree, construct the binary tree.
Solution
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {number[]} inorder * @param {number[]} postorder * @return {TreeNode} */ var buildTree = function(inorder, postorder) { if(inorder.length === 0 || postorder.length === 0) returnnull; let root = new TreeNode(postorder.pop()), index = inorder.indexOf(root.val); root.right = buildTree(inorder.slice(index + 1), postorder); root.left = buildTree(inorder.slice(0, index), postorder); return root; };
95. 96. Unique Binary Search Trees
Description
Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.
/** * @param {number} n * @return {number} */ var numTrees = function(n) { let num = Array(n + 1).fill(0); num.splice(0, 4, ...[1, 1, 2, 5]); if(n < 4) return num[n]; for(let i = 4; i <= n; i++) { for(let k = 1; k <= i; k++) { num[i] += num[k - 1] * num[i - k]; } } return num[n]; };
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {number} n * @return {TreeNode[]} */ var generateTrees = function(n) { if(n === 0) return []; return trees(1, n); }; functiontrees(start, end) { let sub = []; if(start > end) { sub.push(null); return sub; } for(let k = start; k <= end; k++) { let left = trees(start, k - 1); let right = trees(k + 1, end); for(let i of left) { for(let j of right) { let node = new TreeNode(k); node.left = i; node.right = j; sub.push(node); } } } return sub; }
98. Validate Binary Search Tree
Description
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Solution
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {boolean} */ var isValidBST = function(root) { functionisValid(root, min, max) { if(!root) returntrue; return root.val > min && root.val < max && isValid(root.left, min, root.val) && isValid(root.right, root.val, max); } return isValid(root, -Infinity, Infinity); };
104. Maximum Depth of Binary Tree
Description
Given a binary tree, find its maximum depth.
Solution
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number} */ var maxDepth = function(root) { if(!root) return0; let maxLeft = maxDepth(root.left), maxRight = maxDepth(root.right); returnMath.max(maxLeft, maxRight) + 1; };
572. Subtree of Another Tree
Description
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.