leetcode : mergeTrees(递归)
来源:互联网 发布:编程开发是怎样的过程 编辑:程序博客网 时间:2024/06/17 15:15
题:输入两颗二叉树,将它们合成为一颗二叉树,合成规则如下,
The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { if(t2==nullptr)return t1; if(t1==nullptr)return t2; t1->val+=t2->val; t1->left=mergeTrees(t1->left,t2->left); t1->right=mergeTrees(t1->right,t2->right); return t1; }};
阅读全文
0 0
- leetcode : mergeTrees(递归)
- Leetcode-Generate Parentheses(递归)
- LeetCode 之递归问题(二)
- [LeetCode]Permutations(回溯&&DFS&&递归!!!!)
- [LeetCode]Permutations II(!!!!!DFS递归&&回溯)
- leetcode 87 Scramble String(递归+剪枝)
- leetcode 53. Maximum Subarray(分治,递归)
- LeetCode 258 Add Digits(技巧 || 递归)
- leetcode 22. Generate Parentheses (暴力递归)
- LeetCode-3(非递归前中后序遍历)
- [LeetCode]Candy 递归
- leetcode GrayCode 递归
- leetcode---Generate Parentheses---递归
- Recursive - leetcode [递归]
- LeetCode专题----递归
- Leetcode 分治&递归
- leetcode--BinaryTreeLevelOrderTraversal II (二叉树层遍历递归实现)
- [LeetCode] World Search(!!!!回溯&&char *代替string&&递归)
- SQLServer--变量的使用
- ansible批量传输文件
- linux基本操作
- vue获取手机验证码60秒倒计时,不能点击按钮
- Windows下Nginx配置SSL实现Https访问(包含证书生成)
- leetcode : mergeTrees(递归)
- 【Hibernate】基本数据类型对照表
- .class生成.dex
- OpenCV部分API翻译--相机标定相关
- IntelliJ IDEA 开发 WebService
- Python学习笔记 4
- 多益笔试总结
- xilinx serdes通道绑定channel bonding
- 性能测试小Tips