当前位置:网站首页>二叉树解题(一)
二叉树解题(一)
2022-07-02 04:17:00 【木苏栀槿】
先理解二叉树的前中后序遍历:
前序遍历=左子树前序遍历+右子树前序遍历
所以中序和后序只要将res.Add(root.val);放到对应位置;
public IList<int> Traversal(TreeNode root){
List<int> res = new List<int>();
if (root == null)
return res;
// 前序位置 res.Add(root.val);
left = Traversal(root.left);
// 中序位置 res.Add(root.val);
right = Traversal(root.right);
// 后序位置 res.Add(root.val);
return res;
}
前序位置在代码进入节点时执行;
后序位置在代码离开节点时执行;
中序位置在代码遍历完左子树,开始遍历右子树前执行;
在这三个特殊的时间点可以做需要的事。
你只需要思考每一个节点应该做什么,其他的不用你管,抛给二叉树遍历框架,递归会对所有节点做相同的操作。
遇到一道二叉树的题目时的通用思考过程是:是否可以通过遍历一遍二叉树得到答案?如果不能的话,是否可以定义一个递归函数,通过子问题(子树)的答案推导出原问题的答案? 如果需要设计到子树信息, 建议使用后续遍历
Leetcode 617.合并二叉树--简单
public TreeNode MergeTrees(TreeNode root1, TreeNode root2)
{
// 一棵树没有,接上另一棵树有,
if (root1 == null)
return root2;
if (root2 == null)
return root1;
// 合并相同位置的值
root1.val +=root2.val;
// 递归合并左右子树
root1.left = MergeTrees(root1.left, root2.left);
root1.right = MergeTrees(root1.right, root2.right);
return root1;
}
Leetcode 669.修剪二叉搜索树--中等
public TreeNode TrimBST(TreeNode root, int low, int high) {
if (root==null) return null;
// 删除左边(root和root.left)所有节点,返回root.right
if (root.val<low)
{
return TrimBST(root.right, low, high);
}
// 删除右边(root和root.right)所有节点,返回root.left
if (root.val > high)
{
return TrimBST(root.left, low, high);
}
// 在[low,hight]区间中不做处理
root.left = TrimBST(root.left, low, high);
root.right = TrimBST(root.right, low, high);
return root;
}
Leetcode 124.二叉树中的最大路径和--困难
int res = int.MinValue;
public int MaxPathSum(TreeNode root)
{
if (root == null) return 0;
Traverse(root);
return res;
}
int Traverse(TreeNode root)
{
if (root == null) return 0;
int left = Math.Max(0, Traverse(root.left));
int right = Math.Max(0, Traverse(root.right));
// 后序位置,更新最大路径和
res = Math.Max(res, root.val + left + right);
// 从根节点root为起点的最大单边路径和
return Math.Max(left,right)+root.val;
}
边栏推荐
- Go language naming specification
- Pytoch --- use pytoch to predict birds
- okcc为什么云呼叫中心比传统呼叫中心更好?
- Li Kou interview question 02.08 Loop detection
- Demonstration description of integrated base scheme
- First acquaintance with P4 language
- Déchirure à la main - tri
- The confusion I encountered when learning stm32
- C语言猜数字游戏
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
猜你喜欢
FAQ | FAQ for building applications for large screen devices
Wpviewpdf Delphi and Net PDF viewing component
Delete the code you wrote? Sentenced to 10 months!
《动手学深度学习》(二)-- 多层感知机
pip 安装第三方库
手撕——排序
PIP installation of third-party libraries
Go language introduction
PR zero foundation introductory guide note 2
Installation et utilisation du lac bleu
随机推荐
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
Pytoch yolov5 runs bug solution from 0:
FAQ | FAQ for building applications for large screen devices
Wechat applet calculates the distance between the two places
"No war on the Western Front" we just began to love life, but we had to shoot at everything
The confusion I encountered when learning stm32
Force buckle 540 A single element in an ordered array
Where can I buy cancer insurance? Which product is better?
【leetcode】81. Search rotation sort array II
手撕——排序
go 语言命名规范
10 minutes to understand CMS garbage collector in JVM
【毕业季·进击的技术er】年少有梦,何惧彷徨
Websites that it people often visit
Lei Jun wrote a blog when he was a programmer. It's awesome
Wpviewpdf Delphi and Net PDF viewing component
C language guessing numbers game
pip 安装第三方库
【提高课】ST表解决区间最值问题【2】
go 变量与常量