diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 929e6eb2..25bbf7e4 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -234,7 +234,15 @@ public: ## Java 递归法: - +```java +class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q); + if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q); + return root; + } +} +``` 迭代法: ```java diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index b8a1ae87..b12d40aa 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -293,6 +293,32 @@ class Solution { } } ``` +```java +// 解法2 +class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) return root; + if (root.val == key) { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } else { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + cur.left = root.left; + root = root.right; + return root; + } + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; + } +} +``` ## Python diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index aeddc600..22d4f4ac 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -429,6 +429,44 @@ class Solution { } } ``` +迭代法 +```java +class Solution { + public int[] findMode(TreeNode root) { + TreeNode pre = null; + Stack stack = new Stack<>(); + List result = new ArrayList<>(); + int maxCount = 0; + int count = 0; + TreeNode cur = root; + while (cur != null || !stack.isEmpty()) { + if (cur != null) { + stack.push(cur); + cur =cur.left; + }else { + cur = stack.pop(); + // 计数 + if (pre == null || cur.val != pre.val) { + count = 1; + }else { + count++; + } + // 更新结果 + if (count > maxCount) { + maxCount = count; + result.clear(); + result.add(cur.val); + }else if (count == maxCount) { + result.add(cur.val); + } + pre = cur; + cur = cur.right; + } + } + return result.stream().mapToInt(Integer::intValue).toArray(); + } +} +``` ## Python diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index ae6719ec..b19a0dd2 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -171,7 +171,34 @@ class Solution { } } ``` +迭代法-中序遍历 +```java +class Solution { + TreeNode pre; + Stack stack; + public int getMinimumDifference(TreeNode root) { + if (root == null) return 0; + stack = new Stack<>(); + TreeNode cur = root; + int result = Integer.MAX_VALUE; + while (cur != null || !stack.isEmpty()) { + if (cur != null) { + stack.push(cur); // 将访问的节点放进栈 + cur = cur.left; // 左 + }else { + cur = stack.pop(); + if (pre != null) { // 中 + result = Math.min(result, cur.val - pre.val); + } + pre = cur; + cur = cur.right; // 右 + } + } + return result; + } +} +``` ## Python 递归