【算法】 二叉树遍历

二叉树的遍历一般有4种:前序遍历,后续遍历,中序遍历,还有个层序遍历。

1. 前序遍历:先根节点,在递归遍历左子树,接下来递归右子树

   后序遍历:先递归遍历左子树,接下来递归右子树,最后根节点

   中序遍历:先递归遍历左子树,接下来根节点,最后递归右子树

  上面三种遍历方法左子树一定在右子树前遍历,根据根节点遍历顺序区分前中后,一般用递归的方法最直观快捷。

递归的代码:

/* defination of the binary tree */
struct tree_node
{
	int val;
	struct tree_node *left, *right;
};

typedef struct tree_node* tree;

void pre_order(tree t)
{
	if (t == NULL)
		return;

	dosomething(t); // pre order
	pre_order(t->left);
	//dosomething(t); // in order
	pre_order(t->right);
	//dosomething(t); // post order
}


非递归的代码,前中后遍历的方法有比较大的差别,一个一个展示

前序遍历,借助一个栈

void pre_order_non_recursive(tree t)
{
	// visit the root, and mark it's left child, then push it in the stack
	// when pop a node out the stack, visit its right child, when the stack is
	// empty, visit finish
	stack<struct tree_node*> st;
	struct tree_node * current = t;

	while(current != NULL || !st.empty())
	{
		if (current != NULL) // visit it and makr current = t->left
		{
			dosomething(current);
			st.push(current);
			current = current->left;
		}
		else // pop a node from the stack
		{
			current = st.top(); // current has been visited before push in
			st.pop();
			current = current->right;
		}
	}
}

中序遍历,同样借助一个栈实现

void in_order_non_recursive(tree t)
{
	// 同样借助一个栈来实现,只是操作顺序不同
	// 没遇到一个节点,只进行push操作,并mark左节点
	// 出栈是遍历操作,并mark右节点
	// 空时进行pop操作
	stack<struct tree_node *> st;
	struct tree_node *current = t;
	while(current != NULL || !st.empty())
	{
		if (current != NULL) // push
		{
			st.push(current);
			current = current->left;
		}
		else // NULL
		{
			current = st.top();
			st.pop();
			dosomething(current);
			current = current->right;
		}
	}
}



后序遍历比较复杂

void postOrderIter(struct node *root)
{
    if (!root) return;
    stack<struct node*> s, output;
    s.push(root);
    while (!s.empty()) {
        struct node *curr = s.top();
        output.push(curr);
        s.pop();
        if (curr->left)
            s.push(curr->left);
        if (curr->right)
            s.push(curr->right);
    }
    
    while (!output.empty()) {
        cout << output.top()->data << " ";
        output.pop();
    }
    cout << endl;
}


层序:

leetcode上的题目,借助一个队列实现。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
                vector<vector<int> > re;
                if (root == NULL)
                        return re;

                vector<TreeNode *> queue;
                int front = 0, rear = 1;
                int pre_rear;


                queue.push_back(root);
                while(front < rear)
                {
                        pre_rear = rear;
                        vector<int> current_level;
                        while(front < pre_rear)
                        {
                                current_level.push_back(queue[front]->val);
                                if (queue[front]->left != NULL)
                                {
                                        queue.push_back(queue[front]->left);
                                        ++ rear;
                                }
                                if (queue[front]->right != NULL)
                                {
                                        queue.push_back(queue[front]->right);
                                        ++ rear;
                                }
                                ++ front;
                        }
                        re.push_back(current_level);
                }

                return re;

        
    }
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值