Description
For the given binary tree, return a deep copy of it.
Example
Given a binary tree:
1 / 2 3 / 4 5
Return the new binary tree with same structure and same value:
1 / 2 3 / 4 5
Problem solving: Linked list replication. Recursive method is relatively simple, the code is as follows:
/*** Definition of TreeNode: * public class TreeNode {* public int val; * Public TreeNode left, right; * PU Blic TreeNode (int val) {* This.val = val; * This.left = This.right = null; *} *}*/ Public classSolution {/** * @paramroot:the root of binary tree *@return: Root of New tree*/ PublicTreeNode clonetree (TreeNode root) {//Write your code here if(Root = =NULL){ return NULL; } TreeNode new_root=NewTreeNode (Root.val); New_root.left=Clonetree (Root.left); New_root.right=Clonetree (root.right); returnNew_root; }}
375. Clone Binary Tree "Lintcode java"