114. 二叉树展开为链表 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { //先将左子树拉直,再将右子树拉直,置空左子树,拼接右子树 public void flatten(TreeNode root) { if(root==null) return ; flatten(root.left); flatten(root.right); TreeNode…
二叉树展开为链表 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 将其展开为: class Solution{ public: void flatten(TreeNode* root){ if (root == NULL) return; flatten(root->left); flatten(root->right); TreeNode *temp = root->right; root->right = root->left; root->left = N…
二叉树展开为链表(很详细) 描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \3 4 6将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 变形的后续遍历 直觉是先序遍历,但是节点会丢失,可以使用后续遍历. 我们依次遍历 6 5 4 3 2 1,然后每遍历一个节点就将当前节点的右指针更新为上一个节点. 遍历到 5,把 5 的右指针指向 6.6 <- 5 4 3 2 1. 遍历到 4,把 4 的右指针指向 5.6 <- 5 <- 4 3…
题目描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路 二叉树转化为链表的基本思想是:对于左孩子转化为右孩子:对于右孩子,拼接到根结点左子树最后一个节点作为右孩子.所以在自上而下转化时,对于每个节点要先保存其右孩子,然后记录转为链表后本子树的最后一个节点并返回给上一个根节点. 代码 /** * Definition for a binary tree node. * stru…
目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树,原地将它展开为链表. 示例: 给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解法: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(N…
思路:递归,将左子树变成单链表形式,再将右子树变成单链表形式,最后将左子树单链表的末端连接到右子树单链表表头,将根节点的左孩子置空 void flatten(TreeNode* root) { if (root==nullptr) return; flatten(root->left); // 将左子树变成单链表形式 flatten(root->right); // 将右子树变成单链表形式 if (root->left) // 将左子树单链表的末端连接到右子树单链表表头 { TreeNo…
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNo…
上周通过一位小伙伴,加入了一个氛围很好的小群,人不多,但是大家保持着对知识的渴望,让我很感动. 我自己也有一个群,人数也不多,但是能真正互动起来一起学习,一起进步的,还是太少.所以,现在也在学习如何让自己成为更好的群主,带动群活跃,带动一个社群活跃,带动小伙伴们一起进步,是我的愿景.当然,也不否认现在很多群友正在朝着积极向上的方向走着,我要做的,也是时刻保持对知识的渴望,做到"持续学习". 谁让咱是一名优秀的程序员呢.上周日也学习了一遍递归,还通过一个二叉树的例子来简单介绍了下.我之前…
解法一 可以发现展开的顺序其实就是二叉树的先序遍历.算法和 94 题中序遍历的 Morris 算法有些神似,我们需要两步完成这道题. 将左子树插入到右子树的地方 将原来的右子树接到左子树的最右边节点 考虑新的右子树的根节点,一直重复上边的过程,直到新的右子树为 null public void flattern(TreeNode root){ while(root!=null){ if(root.left==null){ root=root.right; }else{ TreeNode pre=…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right…
Medium! 题目描述: 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路: 这道题要求把二叉树展开成链表,根据展开后形成的链表的顺序分析出是使用先序遍历,那么只要是数的遍历就有递归和非递归的两种方法来求解,这里我们也用两种方法来求解. 首先来看递归版本的,思路是先利用DFS的思路找到最左子节点,然后回到其父节点,把其父节点和右子节点断开,将原左子结点连上父节点的右子节点上,然…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right…
[剑指Offer]合并两个排序的链表 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. Ways 合并两个有序的链表,暴力解法肯定不可取.可以采用下面的这个做法,很通用. # Definition for singly-linked list. # class ListNode…
[剑指Offer]反转链表 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描述: 输入一个链表,反转链表后,输出链表的所有元素. Ways 这个题就是[LeetCode]Reverse Linked List 解题报告这个题.思路也一样. 用temp保存pHead的下一个节点.用newHead保存新的节点的开头.然后把pHead.next只想newHead,再移动一下 三个…
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/find-duplicate-subtrees/description/ 题目描述: Given a binary tree, return all duplicate sub…
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/insertion-sort-list/description/ 题目描述: Sort a linked list using insertion sort. A graphical…
[九度OJ]题目1181:遍历链表 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1181 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1<=n<=1000),接下来的一行包括n个整数. 输出: 可能有多组测试数据,对于每组数据, 将n个整数建立升序链表,之后遍历链表并输出. 样例输入: 4 3 5 7 9 样例输出: 3 5 7 9 Ways 这个题目感觉可以作弊啊--我直接排序不…
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find-anagram-mappings/description/ 题目描述: Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order o…
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-triangle-ii/description/ 题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could yo…
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/bulls-and-cows/description/ 题目描述: You are playing the following Bulls and Cows game with your friend: You write down…
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/network-delay-time/description/ 题目描述: There are N network nodes, labelled 1 to N. Given times…
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/coin-change-2/description/ 题目描述: You are given coins of different denominations and a total amount of money. Write a…
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/ones-and-zeroes/description/ 题目描述: n the computer world, use restricted resource you have to generate maximum benef…
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/my-calendar-ii/description/ 题目描述: Implement a MyCalendarTwo class to store your events. A new event can be added if…
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/is-graph-bipartite/description/ 题目描述: Given an undirected graph, return true if and only if it is bipartite. Re…
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar-iii/description/ 题目描述: Implement a MyCalendarThree class to store your events. A new event can always be added. Your class will have one method, book…
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar-i/description/ 题目描述: Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.…
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/maximum-frequency-stack/description/ 题目描述: Implement FreqStack, a class which simulates the operation of a…
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-plus-sign/description/ 题目描述: In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except thos…
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/monotone-increasing-digits/description/ 题目描述: Given a non-negative integer N, find th…