leetcode—图的遍历

1、图的克隆,利用散列
/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
    if (node==NULL) return NULL;
    queue <UndirectedGraphNode*> graphNode;
    unordered_map<UndirectedGraphNode*,UndirectedGraphNode*> hist;//
    graphNode.push(node);
    UndirectedGraphNode *root, *cur;
    root = new UndirectedGraphNode(node->label);
    hist[node]=root;
    while (!graphNode.empty()){
        cur = graphNode.front();
        graphNode.pop();
        for (int i=0;i<cur->neighbors.size();i++){    //对当前节点的每个邻接点进行遍历
            if (hist.find(cur->neighbors[i])==hist.end()){
                graphNode.push(cur->neighbors[i]);
                UndirectedGraphNode* temp = new UndirectedGraphNode(cur->neighbors[i]->label);
                hist[cur->neighbors[i]] = temp;
            }
        }
        vector<UndirectedGraphNode*> neig;
        for (int i=0;i<cur->neighbors.size();i++){
            neig.push_back(hist[cur->neighbors[i]]);
        }
        hist[cur]->neighbors = neig;    
    }
    return root;
    }
};


/*
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        UndirectedGraphNode *res(NULL);
        if (!node) return res;
        unordered_map<int, UndirectedGraphNode *> hash;
        res=new UndirectedGraphNode(node->label);
        hash[node->label]=res;
        DFS(node,res,hash);
        return res;
    }
    void DFS(UndirectedGraphNode *src, UndirectedGraphNode *target, unordered_map<int, UndirectedGraphNode *> &hash) {
        for (int in(0);in<src->neighbors.size();in++) {
            if (hash.find(src->neighbors[in]->label)==hash.end()) { //This is a new node
                target->neighbors.push_back(new UndirectedGraphNode(src->neighbors[in]->label));
                hash[src->neighbors[in]->label]=target->neighbors.back();
                DFS(src->neighbors[in], target->neighbors.back(),hash);
            } else { //Otherwise just put the existed node into your neighbors list
                target->neighbors.push_back(hash[src->neighbors[in]->label]);
            }
        }
    }
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于一棵树的后序遍历,我们首先遍历左子树,然后遍历右子树,最后访问根节点。根据这个顺序,我们可以借助一个栈来完成。 假设要输出根节点root到某一结点target的路径,首先,我们将root节点入栈。然后,进行以下循环: 1. 将栈顶节点弹出; 2. 如果栈顶节点的值等于target的值,则说明找到了目标结点,结束循环; 3. 如果栈顶节点的右子树不为空且右子树未被访问过,则将右子树入栈; 4. 如果栈顶节点的左子树不为空且左子树未被访问过,则将左子树入栈; 5. 如果栈顶节点的左右子树都为空,说明该结点是叶子节点,将其出栈。 最终,栈中保存的结点序列就是根节点到目标结点的路径。 下面是一个示例代码,假设树的节点定义如下: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None ``` ```python def postorderPath(root, target): if root is None: return [] stack = [] result = [] visited = set() stack.append(root) while stack: node = stack[-1] if node in visited: stack.pop() result.pop() else: visited.add(node) result.append(node.val) if node.val == target: break if node.right and node.right not in visited: stack.append(node.right) if node.left and node.left not in visited: stack.append(node.left) return result ``` 这样,调用`postorderPath(root, target)`函数即可得到根节点root到目标结点target的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值