Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python depth first search

# left to right, pre-order depth first tree search, iterative. O(n) time/space
def depthFirstSearch(root):
    st = [root]
    while st:
        current = st.pop()
        print(current)
        if current.right: st.append(current.right) 
        if current.left: st.append(current.left)
 
PREVIOUS NEXT
Tagged: #python #depth #search
ADD COMMENT
Topic
Name
6+1 =