leetcode-剑指36-OK

// language c
// 剑指36
// https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/


/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;

    Node() {}

    Node(int _val) {
        val = _val;
        left = NULL;
        right = NULL;
    }

    Node(int _val, Node* _left, Node* _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
public:
	vector<Node*> order;
	void op(Node* root){
		if(root==NULL)
			return;
		op(root->left);
		order.push_back(root);
		op(root->right);
	}


    Node* treeToDoublyList(Node* root) {
        if(root==NULL)
        	return NULL;
        op(root);
        int len = order.size();
        Node* res[len];
        int next = len-1;
        while(!order.empty()){
        	res[next--] = order.back();
        	order.pop_back();
        }
        for(int i =0; i<len; i++){
        	res[i]->left  = res[(i+len-1)%len];
        	res[i]->right = res[(i+1)%len];
        }
        return res[0];
    }
};
原文地址:https://www.cnblogs.com/gallien/p/14391725.html