I used iterative method to traverse the BST tree. This traversal method is swift and can be used to do post-order, pre-order and in-order traversal by changing only the order of
stack.extend([(1, p.right), (0, p), (1, p.left)]) as you can refer in this article I posted below another problem. https://leetcode.com/discuss/80261/iterative-method-kinds-traversal-recursive-method-changing.
class Solution(object): def closestValue(self, root, target): if root is None: return None minDiff = None minV = None stack = [(1, root)] while stack: p = stack.pop() if not p: continue if p == 1: stack.extend([(1, p.right), (0, p), (1, p.left)]) else: num = p.val if minDiff is None: minDiff = abs(num - target) minV = num else: if abs(num - target) < minDiff: minDiff = abs(num - target) minV = num else: break return minV