---
# System prepended metadata

title: 複製 Tree
tags: [學習記錄, C#]

---

###### tags: `學習記錄` `C#`
# 複製 Tree
要完整的複製整棵樹 :evergreen_tree: 
必須要完整走訪過每個節點
Node 要在判斷內 new，不然新複製的數每個最底下的分支會多一個裡面沒東西的 Node
```csharp=
public Node CopyNode(Node originalNode)
{
    Node clonedNode = null;
    if (originalNode != null)
    {
        clonedNode = new Node();
        clonedNode.CurrentValue = originalNode.CurrentValue;
        clonedNode.SetPriorityForClone(originalNode.Proirity);
        clonedNode.Operator = originalNode.Operator;
        clonedNode.LeftNode = CopyNode(originalNode.LeftNode);
        clonedNode.MiddleNode = CopyNode(originalNode.MiddleNode);
        clonedNode.RightNode = CopyNode(originalNode.RightNode);
    }
    return clonedNode;
}
```