var isValidBST = function (root) { let pre = -Infinity returnvalid(root)
functionvalid(root) { if (!root) returntrue if (!valid(root.left)) returnfalse if (root.val <= pre) returnfalse pre = root.val returnvalid(root.right) } };
var lowestCommonAncestor = function (root, p, q) { if (!root || root === p || root === q) { return root } let left = lowestCommonAncestor(root.left, p, q) let right = lowestCommonAncestor(root.right, p, q) //if (!left && !right) return null if (left && right) return root return left ? left : right };
//序列化 var serialize = function (root) { if (!root) return'#_' let result = root.val + '_' result += serialize(root.left) result += serialize(root.right) return result };
//反序列化 var deserialize = function (data) { const arr = data.split('_')
functioncreateTree(arr) { const val = arr.shift() if (val === '#') { returnnull } const head = newTreeNode(val) head.left = createTree(arr) head.right = createTree(arr) return head }