LeetCode 897 递增顺序搜索树
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 897 递增顺序搜索树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://leetcode-cn.com/problems/increasing-order-search-tree/
解決方案
Morris中序遍歷
class Solution {public TreeNode increasingBST(TreeNode root) {TreeNode cur=root,pre=null;TreeNode head = null,tail=null;while(cur!=null){pre=cur.left;if(pre!=null){while(pre.right!=null&&pre.right!=cur){pre=pre.right;}if(pre.right==null){pre.right=cur;cur=cur.left;continue;}else{cur.left=null;}}if(head == null){head = cur;tail = cur;}else{tail.right = cur;tail = cur;}cur=cur.right;}return head;} }參考文章
O(1)空間的Morris中序遍歷解法
總結
以上是生活随笔為你收集整理的LeetCode 897 递增顺序搜索树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《移动应用开发》实验报告——疫情地图
- 下一篇: LeetCode 1114 按序打印