LeetCode 99. Recover Binary Search Tree(修复二叉搜索树)
来源:互联网 发布:整数划分问题c语言 编辑:程序博客网 时间:2024/05/22 04:34
原题网址:https://leetcode.com/problems/recover-binary-search-tree/
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
方法一:发现有错误顺序则交换,知道全部正确。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private List<TreeNode> errors = new ArrayList<>(); private int min(TreeNode root) { if (root.left != null) return min(root.left); return root.val; } private int max(TreeNode root) { if (root.right != null) return max(root.right); return root.val; } private TreeNode rightmost(TreeNode root) { if (root.right != null) return rightmost(root.right); return root; } private TreeNode leftmost(TreeNode root) { if (root.left != null) return leftmost(root.left); return root; } boolean swapped = false; private void swap(TreeNode node1, TreeNode node2) { int temp = node1.val; node1.val = node2.val; node2.val = temp; swapped = true; } private void check(TreeNode root) { if (root.left != null) { if (root.left.val >= root.val) { swap(root, root.left); } check(root.left); if (max(root.left) >= root.val) { TreeNode rightmost = rightmost(root.left); swap(root, rightmost); } } if (root.right != null) { if (root.right.val <= root.val) { swap(root, root.right); } check(root.right); if (min(root.right) <= root.val) { TreeNode leftmost = leftmost(root.right); swap(root, leftmost); } } } public void recoverTree(TreeNode root) { do { swapped = false; check(root); } while (swapped); }}
方法二:分析两种错误。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private void swap(TreeNode n1, TreeNode n2) { int t = n1.val; n1.val = n2.val; n2.val = t; } private TreeNode[] errors = new TreeNode[4]; private int count; private TreeNode prev; private void check(TreeNode root) { if (count == 4) return; if (root.left != null) check(root.left); if (prev != null && prev.val > root.val) { errors[count++] = prev; errors[count++] = root; } prev = root; if (root.right != null) check(root.right); } public void recoverTree(TreeNode root) { check(root); if (count == 2) swap(errors[0], errors[1]); else swap(errors[0], errors[3]); }}
可以把prev作为递归函数参数。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private TreeNode[] errors = new TreeNode[4]; private int count = 0; private TreeNode traverse(TreeNode prev, TreeNode node) { if (count == 4) return node; if (node.left != null) prev = traverse(prev, node.left); if (prev != null && prev.val > node.val) { errors[count++] = prev; errors[count++] = node; } if (node.right == null) return node; return traverse(node, node.right); } public void recoverTree(TreeNode root) { if (root == null) return; traverse(null, root); if (count == 2) { int t = errors[0].val; errors[0].val = errors[1].val; errors[1].val = t; } else { int t = errors[0].val; errors[0].val = errors[3].val; errors[3].val = t; } }}
0 0
- LeetCode 99. Recover Binary Search Tree(修复二叉搜索树)
- LeetCode | Recover Binary Search Tree(恢复二叉搜索树)
- Recover Binary Search Tree(恢复二叉搜索树)
- leetCode 99.Recover Binary Search Tree(修正二叉搜索树) 解题思路和方法
- Leetcode 99. Recover Binary Search Tree 恢复二叉搜索树 解题报告
- LeetCode OJ 之 Recover Binary Search Tree(恢复二叉搜索树)
- lintcode--二叉搜索树交换节点(leetcode--Recover Binary Search Tree)
- 修复二叉查找树 Recover Binary Search Tree
- leetcode 99. Recover Binary Search Tree BST二叉搜索树的两元素交换的恢复 + 中序遍历
- LeetCode-99-Recover Binary Search Tree 二叉树交换结点
- 【修正二叉树】Recover Binary Search Tree
- 二叉树-recover binary search tree
- [leetcode-二叉查找树的修正]--99. Recover Binary Search Tree
- leetcode 99. Recover Binary Search Tree 二叉树非递归遍历,利用stack和morris遍历
- LeetCode 99. Recover Binary Search Tree
- [LeetCode]99.Recover Binary Search Tree
- [Leetcode] 99. Recover Binary Search Tree
- LeetCode --- 99. Recover Binary Search Tree
- 文章标题
- more命令详解
- 通过引用计数实现智能指针(C++ Primer)
- 扩展欧几里德应用 poj 1061
- Android webview 常用配置
- LeetCode 99. Recover Binary Search Tree(修复二叉搜索树)
- Android四大组件(1.1)--Activity的生命周期
- PHP程序员的技术成长规划
- JAVA基础之线程
- Direct3D - Vertex Buffers
- CodeForces 75AFlea travel
- 人人都应该学习并成为优秀的产品经理,因为你要为《你自己》这个产品负责到底
- 微信公众测试号开发改进版---php
- 居民集会(分治法)