【Leetcode-easy-617】Merge Two Binary Trees
来源:互联网 发布:淘宝温湿度控制板 编辑:程序博客网 时间:2024/05/16 12:05
【Leetcode-easy-617】Merge Two Binary Trees
题目
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. 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.
Example 1:Input: Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 Output: Merged tree: 3 / \ 4 5 / \ \ 5 4 7
Note: The merging process must start from the root nodes of both trees.
思路
递归
程序
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 == null) return t2; if (t2 == null) return t1; t1.val += t2.val; t1.left = mergeTrees(t1.left, t2.left); t1.right = mergeTrees(t1.right, t2.right); return t1; }}
阅读全文
0 0
- 【Leetcode-easy-617】Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees (Easy)
- leetcode 617 merge two binary trees
- 【LeetCode】617 Merge Two Binary Trees
- leetcode 617:Merge Two Binary Trees
- leetcode(617). Merge Two Binary Trees
- LeetCode(617) Merge Two Binary Trees
- 【leetcode】 Merge Two Binary Trees
- leetcode[Merge Two Binary Trees]
- Merge Two Binary Trees(leetcode)
- 617. Merge Two Binary Trees Easy
- 617. Merge Two Binary Trees; Difficulty:Easy
- The Solution to Leetcode 617 Merge Two Binary Trees
- [leetcode]617. Merge Two Binary Trees
- leetcode.617.Merge Two Binary Trees
- [leetcode]: 617. Merge Two Binary Trees
- [Leetcode]617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees (C++)
- 欢迎使用CSDN-markdown编辑器
- 10月14号总结
- leetcode之二叉树类之路径和系列-----112/113/124/257/437 path sum(牵扯附加OJ572和OJ100, 子树和子拓扑)
- python学习:序列
- c#调用win32 API函数修改系统时间
- 【Leetcode-easy-617】Merge Two Binary Trees
- win10 + VS2013 + Qt5.6.0安装与环境配置
- 新手必学的matlab常用
- Incremental Learning of Object Detectors without Catastrophic Forgetting 论文阅读
- Linux内核很吊之 module_init解析 (下)
- XML三种解析方式简介以及JAVA实现
- Java23种设计模式学习笔记【目录总贴】
- 实验二 线性表综合实验(1)
- 解决无法启动程序,因计算机中丢失api-ms-win-crt-runtime.dll的问题