/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//层次遍历 leetcode 102
//思路:把一层节点加入队列中,让后遍历这层节点的同时在队列中加入该节点的左右孩子节点
vector<int> layerTravel(TreeNode* root){
vector<int> res;
if (root == NULL)return res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()){
//计算当前队列中含有多少元素
int size = q.size();
//从头部依次取出size个元素
for (int i = 0; i < size; i++){
root = q.front();
q.pop();
res.push_back(root->val);
//将下一层的元素塞入队列
if (root->left)q.push(root->left);
if (root->right)q.push(root->right);
}
}
return res;
}
//先序遍历 leetcode 144
//思路:1.往左走到底,在往左走的过程中遍历经过的节点并压入一个栈中。
2.取出栈顶节点,压入该节点的右孩子节点,回到1。
vector<int> preOrderTravel(TreeNode* root){
vector<int> res;
if (root == NULL)return res;
stack<TreeNode*> s;
while (root || !s.empty()){
while (root){
res.push_back(root->val);
s.push(root);
root = root->left;
}
root = s.top();
s.pop();
root = root->right;
}
return res;
}
//中序遍历,leetcode 94
//思路:1.往左走到底,在往左走的过程中将经过的节点压入一个栈中。
2.取出栈顶节点,遍历该节点,压入该节点的右孩子节点,回到1。
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
return res;
stack<TreeNode *> st;
st.push(root);
TreeNode *p = root;
while(!st.empty())
{
while(p->left)
{
st.push(p->left);
p = p->left;
}
while(!st.empty())
{
p = st.top();
st.pop();
res.push_back(p->val);
if(p->right)
{
p = p->right;
st.push(p);
break;
}
}
}
return res;
}
};
//后续遍历 leetcode 145
vector<int> postOrderTravel(TreeNode* root){
vector<int> res;
if (root == NULL)return res;
stack<TreeNode*> s;
s.push(root);
TreeNode* head = root;
while (!s.empty()){
TreeNode* t = s.top();
if (!t->left&&!t->right || t->left == head || t->right == head){
res.push_back(t->val);
s.pop();
head = t;
}
else{
if (t->right)s.push(t->right);
if (t->left)s.push(t->left);
}
}
return res;
}
int main(){
return 0;
}
python实现
import queue
class Node(object):
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
'''
层次遍历:先把当前层节点加入队列,然后逐个弹出当前层的节点,
在弹出的过程中遍历这个节点并向队列压入该节点的孩子节点
'''
def layer_travel(root, result):
if root == None:
return
q = queue.Queue()
q.put(root)
while not q.empty():
size = q.qsize()
for _ in range(size):
p = q.get()
result.append(p.value)
if p.left != None:
q.put(p.left)
if p.right != None:
q.put(p.right)
'''
先序遍历:1.往左走到底,在往左走的过程中遍历经过的节点并压入一个栈中。
2.取出栈顶节点,压入该节点的右孩子节点,回到1。
'''
def preorder_travel(root, result):
if root == None:
return
p = root
stack = []
stack.append(p)
result.append(p.value)
while len(stack) > 0:
while p.left != None:
p = p.left
stack.append(p)
result.append(p.value)
while len(stack) > 0:
p = stack.pop()
if p.right != None:
p = p.right
stack.append(p)
result.append(p.value)
break
'''
中序遍历:1.往左走到底,在往左走的过程中将经过的节点压入一个栈中。
2.取出栈顶节点,遍历该节点,压入该节点的右孩子节点,回到1。
'''
def inorder_travel(root, result):
if root == None:
return
p = root
stack = []
stack.append(p)
result.append(p.value)
while len(stack) > 0:
while p.left != None:
p = p.left
stack.append(p)
while len(stack) > 0:
p = stack.pop()
result.append(p.value)
if p.right != None:
p = p.right
stack.append(p)
break
'''
后序遍历
'''
def postorder_travel(root, result):
if root == None:
return
head = root #存储上一步被弹出的节点
stack = []
stack.append(head)
while len(stack) > 0:
p = stack[-1]
#当该节点没有儿子节点或儿子节点已经被弹出时,弹出该节点
if (p.left == None and p.right == None) or\
p.left == head or p.right == head:
result.append(p.value)
head = p
stack.pop()
else:
if p.right != None:
stack.append(p.right)
if p.left != None:
stack.append(p.left)