当前位置:网站首页>二叉树解题(二)
二叉树解题(二)
2022-07-02 04:17:00 【木苏栀槿】
Leetcode 226._翻转二叉树
// 方法一:遍历二叉树
public TreeNode InvertTree(TreeNode root)
{
// 遍历二叉树,交换每个节点的子节点
Traverse(root);
return root;
}
// 二叉树遍历函数
void Traverse(TreeNode root) {
if (root == null) return;
// 每一个节点需要做的事就是交换它的左右子节点
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
// 遍历框架,去遍历左右子树的节点
Traverse(root.left);
Traverse(root.right);
}
// 方法二:递归函数分解问题
public TreeNode InvertTree(TreeNode root)
{
if (root == null) return root;
// 翻转左右子树
TreeNode left = InvertTree(root.left);
TreeNode right = InvertTree(root.right);
// 交换左右子节点
root.left = right;
root.right = left;
// 以 root 为根的这棵二叉树已经被翻转,返回 root
return root;
}
Leetcode 116._填充每个节点的下一个右侧节点指针
public Node Connect(Node root)
{
if (root == null) return null;
Traverse(root.left,root.right);
return root;
}
void Traverse(Node left,Node right)
{
if (left == null|| right == null) return;
// 将传入的两个节点穿起来
left.next = right;
// 连接相同父节点的两个子节点
Traverse(left.left,left.right);
Traverse(right.left, right.right);
// 连接跨越父节点的两个子节点
Traverse(left.right, right.left);
}
Leetcode 114._二叉树展开为链表
public void Flatten(TreeNode root)
{
if (root == null) return;
Traverse(root);
}
void Traverse(TreeNode root)
{
if (root == null) return;
// 遍历拉平左右子树
Traverse(root.left);
Traverse(root.right);
// 后序位置
TreeNode left = root.left;
TreeNode right = root.right;
// 将左子树作为右子树
root.left = null;
root.right = left;
// 将右子树添加到当前右子树末尾
TreeNode p = root;
while (p.right!=null)
{
p = p.right;
}
p.right = right;
}
边栏推荐
- Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
- Www2022 | know your way back: self training method of graph neural network under distribution and migration
- Lei Jun wrote a blog when he was a programmer. It's awesome
- Feature Engineering: summary of common feature transformation methods
- [C language] Dynamic Planning --- from entry to standing up
- Actual combat | use composite material 3 in application
- Installation et utilisation du lac bleu
- Which product of anti-cancer insurance is better?
- A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
- 文档声明与字符编码
猜你喜欢
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
Pytorch---使用Pytorch实现U-Net进行语义分割
[untitled]
JVM knowledge points
BGP experiment the next day
Li Kou interview question 02.08 Loop detection
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
PIP installation of third-party libraries
Actual combat | use composite material 3 in application
随机推荐
【毕业季·进击的技术er】年少有梦,何惧彷徨
Hand tear - sort
Force buckle 540 A single element in an ordered array
IDEA xml中sql没提示,且方言设置没用。
5g era is coming in an all-round way, talking about the past and present life of mobile communication
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Wechat applet calculates the distance between the two places
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
SQL:常用的 SQL 命令
Wechat applet pull-down loading more waterfall flow loading
Learn more about materialapp and common attribute parsing in fluent
Message mechanism -- message processing
Recyclerview add header
Yyds dry inventory compiler and compiler tools
Pytorch---使用Pytorch实现U-Net进行语义分割
Go branch and loop
Pytorch---使用Pytorch进行图像定位
Common locks in MySQL