6.leetCode617:Merge Two Binary Trees
来源:互联网 发布:js children 编辑:程序博客网 时间:2024/06/01 09:16
题目:
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.
思路1:可以新建一棵树M,为A和B的合并树。M的每个节点值是A、B对应节点的值之和。若某一棵树对应节点为空,则M种对应节点的值为另一棵树的节点值。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { TreeNode root; TreeNode left_1 = null, left_2 = null; TreeNode right_1 = null, right_2 = null; if(t1 == null && t2 == null) return null; else if(t1 != null && t2 != null) { root = new TreeNode(t1.val + t2.val); left_1 = t1.left; left_2 = t2.left; right_1 = t1.right; right_2 = t2.right; } else if(t1 != null && t2 == null) { root = new TreeNode(t1.val); left_1 = t1.left; right_1 = t1.right; } else { root = new TreeNode(t2.val); left_2 = t2.left; right_2 = t2.right; } root.left = mergeTrees(left_1, left_2); root.right = mergeTrees(right_1, right_2); return root; }}
思路2:要合并两棵树A和B,以A为基础,将A、B中对应的节点的值进行相加,并更新A的节点值。
class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if(t1 ==null && t2 ==null) return null; else if(t1 == null) return t2; else 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; }}
思路3:使用一个栈来存储对应的节点对。从根节点开始,若对应节点都不为空,则相加;若一对对应节点中的其中一个节点为空,则为不为空的节点值。当前节点处理完后,把其左孩子节点和右孩子节点的对应节点对都放入栈中。
class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 == null) return t2; Stack < TreeNode[] > stack = new Stack < > (); stack.push(new TreeNode[] {t1, t2}); while (!stack.isEmpty()) { TreeNode[] t = stack.pop(); if (t[0] == null || t[1] == null) { continue; } t[0].val += t[1].val; if (t[0].left == null) { t[0].left = t[1].left; } else { stack.push(new TreeNode[] {t[0].left, t[1].left}); } if (t[0].right == null) { t[0].right = t[1].right; } else { stack.push(new TreeNode[] {t[0].right, t[1].right}); } } return t1; }}
- 6.leetCode617:Merge Two Binary Trees
- Leetcode617. Merge Two Binary Trees
- leetcode617: Merge Two Binary Trees
- leetcode617. Merge Two Binary Trees
- LeetCode617. Merge Two Binary Trees
- python--leetcode617. Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 想自己写个cocos2dx的编译脚本(一)
- 用p5.js制作烟花特效
- iOS开发 防止UIButton,cell等重复点击
- display和visibility的区别
- Android内存泄漏及分析
- 6.leetCode617:Merge Two Binary Trees
- #Java 核心技术卷一阅读笔记# 第七章 异常、断言和日志
- 电子网络营销能给你带来什么益处
- 2017都要过去了,还不知道服务设计?
- iOS 启动耗时统计
- MySql中汉字排序
- 基于springboot和dubbo搭建SOA服务
- Linux spi驱动框架之执行流程-nuc970-att7022
- spring 之 RestTemplate 用法