活跃农民
- 积分
- 432
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-8-4
- 最后登录
- 1970-1-1
|
5.13 刷题
144. Binary Tree Preorder Traversal. There are two kinds of memories in computer, Stack and Heap. Stack is small while Heap is large. If we use recursion here and there are too many recursion calls, it may cause StackOverFlow. So we will want to use Heap, which means we need to use iterative way. But the idea of iteration and recursion methods are same. We may simulate the recursion process in Heap, which means we will create a Stack in Heap. The iterative way to solve preorder traversal problem is similar with BFS. We use an auxiliary data structure to store the node. Expand the top of stack and generate the two children nodes, right first then left, into the stack. In this way, we can print the Binary tree preorder.
94. Binary Tree Inorder Traversal. Here I use a helper pointer. Helper pointer should point to the next node we want to scan. And every scanned node we will push it into the stack. If helper pointer is null, which means there is no next node and the last node we put into the stack is the final node, we will pop the top node in stack and print it. After that, the helper pointer should point to another 'next' node. What is the next node? What's the path of helper pointer? Since this is Binary Tree Inorder Traversal, the helper pointer should follow the in-order path, which is left - root - right. At first, the next node should always be the left node. So helper pointer should always go down left. Then, when helper pointer is null, which means the current root has no left child, we just pop it from the stack and print. Then the next node should be the right child of current node so we let the helper pointer point to it.
145. Binary Tree Postorder Traversal. Still use iterative way. We need maintain a previous node, storing the previous visiting node, so that we know what the direction we're visiting now and what visiting next. Each time we get the top element of stack, as the current node, and if the previous node is null, the current node is the root node and we need go down with priority in left child. if the previous node is the parent of current node, we need go down with priority in left child. If the previous node is the left child of current node, which means we have completed the traversal of left sub tree, we need turn to right subtree. If the previous node is the right child of current node, which means both children we have completed, and we go up. Each time if there is no left and right nodes, we go up, and print.
102. Binary Tree Level Order Traversal. BFS problem based on Queue. For level order traversal, we need to keep the size of current level before expanding and generating. Then putting all expanded nodes into current solution. Do this until the Queue is empty. This is a basic problem.
100. Same Tree. Recursive way to solve this problem. First check the base case: 1) if the two root are both null? 2) if there is only one null root? 3) if the values of two root are equal? After the base case checking, and since two roots have left and right children and each child node may be the root of subtree, we will call recursion function to compare two left children and two right children. |
|