Skip to main content

Posts

Showing posts from June, 2020

The Code Pattern Under LeetCode: Tree (1)

Almost all problems of tree in leetcode can be converted into tree node traversal problem.  In the review, there is summary for all kinds of traversal ways;                                                                                from leetcode: all reach 1-2-3-4-5 order and for each of traversal way, we can also have the variation for the access from right -> left.  So as the basic skill, we need knew to how travel the whole tree using both iteration and recursion for each of traversal way.  Let's use the simple example from leetcode for detail explanation.    Once we convert the problem to tree visiting way, it is pretty straightforward. when traveling the tree inorder, the numbers will be ordered in sequence. when we write the code, firstly we write the code to trave tree inorder. and we add minor check  to check if the visited numbers is in sequence..  the code for InOrder visit: alternatively, the iteration version(remember it as your start point for most problem reso