超酷的中序遍历

本文介绍了一种使用占位符和栈实现的二叉树中序遍历方法。通过创建临时节点来保存遍历状态,使得遍历过程更加直观。文章详细展示了算法的具体实现过程。
#include <stack>
#include <utility>
#include <string>
#include <sstream>

using namespace std;

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};

class Solution {
public:
    void inorder(TreeNode* root) {
        map<TreeNode*, TreeNode*> placeholders;
        stack<TreeNode*> stk;
        
        if ( root != NULL ) {
            TreeNode* placeholder = new TreeNode( root->val );
            placeholder->left = root->left;
            placeholder->right = root->right;
            placeholders[placeholder] = root;
            
            stk.push( placeholder );
            
            while ( !stk.empty() ) {
                TreeNode *top_elem = stk.top();
                stk.pop();
                
                if ( placeholders.find(top_elem) != placeholders.end() ) {
                    // is a placeholder
                    if ( top_elem->left != NULL ) {
                        if ( top_elem->right != NULL ) {
                            placeholder = new TreeNode( top_elem->right->val );
                            placeholder->left = top_elem->right->left;
                            placeholder->right = top_elem->right->right;
                            placeholders[placeholder] = top_elem->right;
                            
                            stk.push( placeholder );
                        }
                        
                        // important!
                        stk.push( placeholders[top_elem] );
                        
                        placeholder = new TreeNode( top_elem->left->val );
                        placeholder->left = top_elem->left->left;
                        placeholder->right = top_elem->left->right;
                        placeholders[placeholder] = top_elem->left;
                        
                        stk.push( placeholder );
                    }
                    else {
                        cout << top_elem->val << " ";
                        
                        if ( top_elem->right != NULL ) {
                            placeholder = new TreeNode( top_elem->right->val );
                            placeholder->left = top_elem->right->left;
                            placeholder->right = top_elem->right->right;
                            placeholders[placeholder] = top_elem->right;
                            
                            stk.push( placeholder );
                        }
                    }
                }
                else { // is not a placeholder
                    cout << top_elem->val << " ";
                }
            }
        }
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值