速读摘要文章源自JAVA秀-https://www.javaxiu.com/10723.html
为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选~!我们从根节点开始,递归地对树进行遍历,并从叶子结点先开始翻转。文章源自JAVA秀-https://www.javaxiu.com/10723.html
原文约 2187 字 | 图片 2 张 | 建议阅读 5 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战226:翻转二叉树
小猿同学 程序IT圈 文章源自JAVA秀-https://www.javaxiu.com/10723.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/10723.html
#Leecode刷题文章源自JAVA秀-https://www.javaxiu.com/10723.html
110个文章源自JAVA秀-https://www.javaxiu.com/10723.html
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !文章源自JAVA秀-https://www.javaxiu.com/10723.html
文章源自JAVA秀-https://www.javaxiu.com/10723.html
今天和大家聊的问题叫做 翻转二叉树,我们先来看题面:文章源自JAVA秀-https://www.javaxiu.com/10723.html
https://leetcode-cn.com/problems/invert-binary-tree/文章源自JAVA秀-https://www.javaxiu.com/10723.html
Given the root of a binary tree, invert the tree, and return its root.文章源自JAVA秀-https://www.javaxiu.com/10723.html
翻转一棵二叉树。文章源自JAVA秀-https://www.javaxiu.com/10723.html
示例
文章源自JAVA秀-https://www.javaxiu.com/10723.html
解题
文章源自JAVA秀-https://www.javaxiu.com/10723.html
递归解决文章源自JAVA秀-https://www.javaxiu.com/10723.html
这是一道很经典的二叉树问题。显然,我们从根节点开始,递归地对树进行遍历,并从叶子结点先开始翻转。如果当前遍历到的节点 root 的左右两棵子树都已经翻转,那么我们只需要交换两棵子树的位置,即可完成以root 为根节点的整棵子树的翻转。文章源自JAVA秀-https://www.javaxiu.com/10723.html
文章源自JAVA秀-https://www.javaxiu.com/10723.html
class Solution {public: TreeNode* invertTree(TreeNode* root) { if(root ==NULL) return root; TreeNode* node = invertTree(root->left); root->left = invertTree(root->right); root->right = node; return root; }};文章源自JAVA秀-https://www.javaxiu.com/10723.html
迭代法文章源自JAVA秀-https://www.javaxiu.com/10723.html
本质思想是,左右节点进行交换,循环翻转每个节点的左右子节点,将未翻转的子节点存入队列中,循环直到栈里所有节点都循环交换完为止。文章源自JAVA秀-https://www.javaxiu.com/10723.html
public class Solution { public TreeNode invertTree(TreeNode root) { Queue<TreeNode> q = new LinkedList<TreeNode>(); if(root!=null) q.offer(root); while(!q.isEmpty()){ TreeNode curr = q.poll(); TreeNode tmp = curr.right; curr.right = curr.left; curr.left = tmp; if(curr.left!=null) q.offer(curr.left); if(curr.right!=null) q.offer(curr.right); } return root; }}文章源自JAVA秀-https://www.javaxiu.com/10723.html
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。文章源自JAVA秀-https://www.javaxiu.com/10723.html
上期推文:文章源自JAVA秀-https://www.javaxiu.com/10723.html
文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode1-220题汇总,希望对你有点帮助!文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战221:最大正方形文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战222:完全二叉树的节点个数文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战223:矩形面积文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战224:基本计算器文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战225:用队列实现栈文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战226:翻转二叉树文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战227:基本计算器 II文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战228:汇总区间文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战229:求众数 II文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战230:二叉搜索树中第K小的元素文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战231:2的幂文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战232:用栈实现队列文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战233:数字 1 的个数文章源自JAVA秀-https://www.javaxiu.com/10723.html
LeetCode刷题实战234:回文链表文章源自JAVA秀-https://www.javaxiu.com/10723.html
文章源自JAVA秀-https://www.javaxiu.com/10723.html

评论