Leetcode 617.合并二叉树

合并二叉树

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。

你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

示例 1:

输入:

输出:

合并后的树:

注意: 合并必须从两个树的根节点开始。

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
12         if(t1!=null&&t2!=null) t1.val=t1.val+t2.val;
13         if(t1==null&&t2!=null){
14             t1=new TreeNode(t2.val);
15         }
16         if(t2!=null){
17             t1.left=mergeTrees(t1.left,t2.left);
18             t1.right=mergeTrees(t1.right,t2.right);
19         }
20         return t1;
21     }
22 }

原文地址:https://www.cnblogs.com/kexinxin/p/10381427.html

时间: 2024-10-08 19:26:35

Leetcode 617.合并二叉树的相关文章

leetcode.617 合并两个二叉树

题目描述:给予两个二叉树  t1 , t2  ,合并他们. 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 t

leadcode的Hot100系列--617. 合并二叉树

合并,就是两个树的结构交集部分,数据相加,否则,取非空部分. 所以,这里相当于是对两棵树同时遍历: 如果两棵树节点都不为空,则数据相加, 否则,直接指针把不为空的节点复制过来. 注:这里没有申请内存,而直接对原有的树进行改造,这样可以节省申请内存的时间,且节省一些内存. struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2){ struct TreeNode *pTemp = NULL; if ((NULL ==

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)

题目: 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,

[LeetCode] 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

LeetCode 数组转二叉树 C#

把LeetCode二叉树题目的测试数组,转换成二叉树 class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } } class Tree { public static TreeNode CreateNode(int? val) { if (val == null) return null; return new TreeNod

LeetCode #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

力扣——合并二叉树

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点. 示例 1: 输入: Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出: 合并后的树: 3 / 4 5 / \ \ 5 4 7 /** * Definition for a binary tree node.

LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n