9月 6 2017 Online Judge►LeetCode [LeetCode] 98 - Validate Binary Search Tree 題意判斷是否為二元搜尋樹。 解法二元搜尋樹對於任一節點來說,左子樹必小於且右子樹必大於該節點。 利用 Inorder Traversal 的特性「左子樹->根->右子樹」,判斷該結果是否為升序排列即可。 程式12345678910111213141516171819202122232425262728293031323334/** * 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) { let stack = []; let result = []; let node = root; while( stack.length !== 0 || node !== null ){ while ( node !== null ){ stack.push(node); node = node.left; } node = stack.pop(); result.push(node.val); node = node.right; } for ( let i = 1 ; i < result.length ; i ++ ){ if ( result[i] <= result[i-1] ){ return false; } } return true;}; Newer [LeetCode] 94 - Binary Tree Inorder Traversal Older [LeetCode] 102 - Binary Tree Level Order Traversal