Today I uploaded my first solution on Leetcode. Cheers!
Here is the content~
Recursive programming is easy and straightforward.
- Base condition: return (empty list) when input is None.
- Step condition: if input is not None, append current node’s value to the rList. And then proceed to Pre-traversal the Left and the Right.
In this way, whatever the input is, its state will gradually change from initial state into base condition state and end the recursive program.
CLASS SOLUTION: DEF PREORDERTRAVERSAL(SELF, ROOT): """ :TYPE ROOT: TREENODE :RTYPE: LIST[INT] """ RLIST =  IF ROOT == NONE: RETURN RLIST ELSE: RLIST.APPEND(ROOT.VAL) IF ROOT.LEFT != NONE: FOR VAL IN SELF.PREORDERTRAVERSAL(ROOT.LEFT): RLIST.APPEND(VAL) IF ROOT.RIGHT != NONE: FOR VAL IN SELF.PREORDERTRAVERSAL(ROOT.RIGHT): RLIST.APPEND(VAL) RETURN RLIST