LintCode-375.克隆二叉树
生活随笔
收集整理的這篇文章主要介紹了
LintCode-375.克隆二叉树
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
克隆二叉樹
深度復(fù)制一個(gè)二叉樹。
給定一個(gè)二叉樹,返回一個(gè)他的 克隆品 。
樣例
給定一個(gè)二叉樹:
返回其相同結(jié)構(gòu)相同數(shù)值的克隆二叉樹:
標(biāo)簽
二叉樹
code
/*** Definition of TreeNode:* class TreeNode {* public:* int val;* TreeNode *left, *right;* TreeNode(int val) {* this->val = val;* this->left = this->right = NULL;* }* }*/ class Solution { public:/*** @param root: The root of binary tree* @return root of new tree*/TreeNode* cloneTree(TreeNode *root) {// Write your code hereif(root != NULL) {TreeNode* pNewRoot = new TreeNode(root->val);if(root->left != NULL)pNewRoot->left = cloneTree(root->left);if(root->right != NULL)pNewRoot->right = cloneTree(root->right);return pNewRoot;}return NULL;} };轉(zhuǎn)載于:https://www.cnblogs.com/libaoquan/p/6807002.html
總結(jié)
以上是生活随笔為你收集整理的LintCode-375.克隆二叉树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让Lua支持Linq吧
- 下一篇: zoj How Many Shortes