Original link: leetcode-cn.com/problems/va…

Answer:

  1. Referring to the animation of middle order traversal in the official solution, we know that the order of middle order traversal is from left to right.
  2. That is, you can iterate recursively, output the values of all the nodes, and then determine whether the value of the node is increasing.
  3. Middle order traversal of binary trees, recursion, JavaScript, detailed annotation
/ * * *@param {TreeNode} root
 * @return {boolean}* /
var isValidBST = function (root) {
  let values = []; // Save the sequence traversal result

  // Middle order traversal binary tree
  function traversal(node) {
    if (node) {
      traversal(node.left);
      values.push(node.val);
      traversal(node.right);
    }
  }
  traversal(root);

  // Iterate over the result array to see if the array is incremented
  for (let i = 1; i < values.length; i++) {
    // If the current value <= the previous value, it is non-incrementing
    if (values[i] <= values[i - 1]) {
      return false; }}// If the loop exits normally, the nodes of the binary tree increase from left to right, that is, the binary search tree
  return true;
};
Copy the code