LeetCode:二叉树相关应用
LeetCode:二叉树相关应用
基础知识
617.归并两个二叉树
题目
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.
分析

以t1树为基础展开归并,首先两树都进行先序遍历,在遍历的过程中,如果发现一方当前节点不存在,则用另一者的节点桥接过来,如果两者都存在,则计算其和。
这里有两个小思考点:
如果t1节点有,而t2节点没有,那么无须进行其他操作,并且t1节点的当前子节点都无需遍历,因为t2全都不存在。同理,t1没有,t2桥接过来,所有的子节点都无需再遍历。
本题主要考察了二叉树的线性存储的先序遍历、递归思想。
标准题解
/**
* 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;
}
}
LeetCode:二叉树相关应用的更多相关文章
- [LeetCode] 二叉树相关题目(不完全)
最近在做LeetCode上面有关二叉树的题目,这篇博客仅用来记录这些题目的代码. 二叉树的题目,一般都是利用递归来解决的,因此这一类题目对理解递归很有帮助. 1.Symmetric Tree(http ...
- 二叉树-你必须要懂!(二叉树相关算法实现-iOS)
这几天详细了解了下二叉树的相关算法,原因是看了唐boy的一篇博客(你会翻转二叉树吗?),还有一篇关于百度的校园招聘面试经历,深刻体会到二叉树的重要性.于是乎,从网上收集并整理了一些关于二叉树的资料,及 ...
- LeetCode二叉树实现
LeetCode二叉树实现 # 定义二叉树 class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- acwing 70-72 剑指OFFER 二叉树相关
地址 https://www.acwing.com/problem/content/66/ https://www.acwing.com/problem/content/67/ https://www ...
- leetcode tree相关题目总结
leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...
- PAT甲级 二叉树 相关题_C++题解
二叉树 PAT (Advanced Level) Practice 二叉树 相关题 目录 <算法笔记> 重点摘要 1020 Tree Traversals (25) 1086 Tree T ...
- [LeetCode] [链表] 相关题目总结
刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...
- LeetCode 二叉树,两个子节点的最近的公共父节点
LeetCode 二叉树,两个子节点的最近的公共父节点 二叉树 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共父亲节点 https://leetcod ...
- leetcode二叉树题目总结
leetcode二叉树题目总结 题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/ 前序遍历(NLR) p ...
随机推荐
- 【Excle】在重复数据中对日期排序并查询最新的一条记录
现在存在以下数据: 需要查询出以下数据 姓名 日期 张三 2017-12-14 李四 2017-12-16 在E1中写入以下公式:=IF(D2=MAX(IF($C$ ...
- Loadrunner中对中文进行UTF-8转码的探索
上一篇 / 下一篇 2010-02-22 15:20:28 查看( 2378 ) / 评论( 2 ) / 评分( 5 / 0 ) 这是一个HTTP接口测试中经常会碰到的问题,目前的服务器采用的都是U ...
- The TTY demystified
http://www.linusakesson.net/programming/tty/index.php The TTY demystified Real teletypes in the 1940 ...
- hector_localization hector_salm rplidar同时编译
1.将hector_localization包clone到src文件夹 进行功能包依赖安装 cd test_ws rosdep update rosdep install --from-paths ...
- MySQL:ERROR 1067 (42000): Invalid default value for 'end_time'
© 版权声明:本文为博主原创文章,转载请注明出处 1.错误截图 2.错误分析 表中的第一个TIMESTAMP列(如果未声明为NULL或显示DEFAULT或ON UPDATE子句)将自动分配DEFAUL ...
- lua学习笔记(十三)
math库 定义在math中 所有三角函数都使用弧度 指数和对数函数 取整函数 伪随机数math.random 调用时没有参数返回0~1之间的随 ...
- oracle中过滤中文字符或者汉字的函数
CREATE OR REPLACE FUNCTION GET_CHINESE(P_NAME IN VARCHAR2) RETURN VARCHAR2 IS V_CODE VARCHAR2 ...
- wc 命令
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- CIA 读书笔记
对此书的评价只有八个字:粗制滥造,到处粘贴. 对于通过表情识别人情绪的教程,最好要有图,图很重要,也最好有案例.
- dede 文章列表页如何倒序排列
{dede:arclist row='6' typeid='18' orderway='asc'} <li>;<a href="[field:arcurl/]"& ...