标签:inline int java render des lint code 链表 copy
For the given binary tree, return a deep copy of it.
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
解题:链表复制。递归解法比较简单,代码如下:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree
* @return: root of new tree
*/
public TreeNode cloneTree(TreeNode root) {
// write your code here
if(root == null){
return null;
}
TreeNode new_root = new TreeNode(root.val);
new_root.left = cloneTree(root.left);
new_root.right = cloneTree(root.right);
return new_root;
}
}
375. Clone Binary Tree【LintCode java】
标签:inline int java render des lint code 链表 copy
原文地址:https://www.cnblogs.com/phdeblog/p/9309219.html