文件名称:preorder_tree_stack
介绍说明--下载内容均来自于网络,请自行研究使用
二叉树非递归先序遍历,使用栈
一直往左走 root->left->left->left…->null,
由于是先序遍历,因此一遇到节点,便需要立即访问;
由于一直走到最左边后,需要逐步返回到父节点访问右节点,
因此必须有一个措施能够对节点序列回溯。
有两个办法:
1.用栈记忆:在访问途中将依次遇到的节点保存下来。由于节点出现次序与恢复次序是反序的,因此是一个先进后出结构,需要用栈。
2.节点增加指向父节点的指针:通过指向父节点的指针来回溯-Nonrecursive preorder binary tree, using the stack has been left to go root-> left-> left-> left ...-> null, as is the preorder, it encountered a node, we need immediate access as has come to the after the left, we need to gradually return to the parent node to access the right node, so there must be able to measure a sequence of nodes backtracking. There are two ways: 1. Stack Memory: The saved nodes sequentially encountered in the access way. Since the node order and restore order in reverse order, so that is a last-out structure, need to use the stack. 2. The node point increase pointer to the parent: by pointing a pointer to the parent node back
一直往左走 root->left->left->left…->null,
由于是先序遍历,因此一遇到节点,便需要立即访问;
由于一直走到最左边后,需要逐步返回到父节点访问右节点,
因此必须有一个措施能够对节点序列回溯。
有两个办法:
1.用栈记忆:在访问途中将依次遇到的节点保存下来。由于节点出现次序与恢复次序是反序的,因此是一个先进后出结构,需要用栈。
2.节点增加指向父节点的指针:通过指向父节点的指针来回溯-Nonrecursive preorder binary tree, using the stack has been left to go root-> left-> left-> left ...-> null, as is the preorder, it encountered a node, we need immediate access as has come to the after the left, we need to gradually return to the parent node to access the right node, so there must be able to measure a sequence of nodes backtracking. There are two ways: 1. Stack Memory: The saved nodes sequentially encountered in the access way. Since the node order and restore order in reverse order, so that is a last-out structure, need to use the stack. 2. The node point increase pointer to the parent: by pointing a pointer to the parent node back
(系统自动生成,下载前可以参看下载内容)
下载文件列表
preorder_tree_stack.cpp