首页 > 其他分享 >反转二叉树演练 leetcode |第 6 部分

反转二叉树演练 leetcode |第 6 部分

时间:2022-08-28 17:56:57浏览次数:89  
标签:递归 反转 leetcode 节点 二叉树 演练

反转二叉树演练 leetcode |第 6 部分

上一个问题

[

有效回文演练 Leetcode |第 5 部分

上一个问题

媒体网

](/@nerdhide/valid-palindrome-walkthrough-leetcode-part-5-8d71c72a6836)

问题陈述:

我们得到一棵二叉树,我们需要反转它。

二叉树是一种数据结构。二叉树由多个节点组成,每个节点以特定方式连接到其他两个节点。例如,较高的值在右边,较低的值在左边,或者较低的值在右边,较高的值在左边。

数据结构最好的部分是因为它以特定的方式组织,我们可以快速执行搜索操作。

反转二叉树意味着改变它们的排列方式。

解决方案

要反转二叉树,我们必须反转每个节点。在我们的解决方案中,首先倒置较大的分支,然后是较小的分支。并使用递归方法来完成此操作

 检查根是否不为空  
 交换root的左右节点  
 反转左节点(递归调用)  
 反转右节点(递归调用)  
 返回根

Python中的解决方案

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明

本文链接:https://www.qanswer.top/1316/41162817

标签:递归,反转,leetcode,节点,二叉树,演练
From: https://www.cnblogs.com/amboke/p/16633223.html

相关文章

  • LeetCode 2186. Minimum Number of Steps to Make Two Strings Anagram II
    原题链接在这里:https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/题目:Youaregiventwostrings s and t.Inonestep,you......
  • LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram
    原题链接在这里:https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/题目:Youaregiventwostringsofthesamelength s and t.......
  • LeetCode 859. Buddy Strings
    原题链接在这里:https://leetcode.com/problems/buddy-strings/题目:Giventwostrings s and goal,return true ifyoucanswaptwolettersin s sotheresult......
  • [Google] LeetCode 359 Logger Rate Limiter
    Designaloggersystemthatreceivesastreamofmessagesalongwiththeirtimestamps.Eachuniquemessageshouldonlybeprintedatmostevery10seconds(i.e......
  • leetcode-793. 阶乘函数后 K 个零
    793.阶乘函数后K个零图床:blogimg/刷题记录/leetcode/793/刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html题目思路首先我们令\(zeta(x)\)为\(x!\)......
  • leetcode-172. 阶乘后的零
    172.阶乘后的零图床:blogimg/刷题记录/leetcode/172/刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html题目思路n!中有几个0与[1,n]中出现多少个5的因数......
  • LeetCode 1166. Design File System
    原题链接在这里:https://leetcode.com/problems/design-file-system/题目:Youareaskedtodesignafilesystem thatallowsyoutocreatenewpathsandassociatet......
  • LeetCode/阶乘后的零
    1.返回尾零数量可以转换为求质因子为2和5数量的较小值,实际上就是求质因子为5的数量classSolution{public:inttrailingZeroes(intn){intans=0;......
  • 【重要】LeetCode 662. 二叉树最大宽度
    题目链接注意事项根据满二叉树的节点编号规则:若根节点编号为u,则其左子节点编号为u<<1,其右节点编号为u<<1|1。一个朴素的想法是:我们在DFS过程中使用两个哈希表......
  • 662. 二叉树最大宽度
    662.二叉树最大宽度给你一棵二叉树的根节点root,返回树的最大宽度。树的最大宽度是所有层中最大的宽度。每一层的宽度被定义为该层最左和最右的非空节点......