Last updated on 2023年7月16日 下午
700.二叉搜索树中的搜索(层序遍历法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode searchBST(TreeNode root, int val) { if(root == null){ return null; }
Queue<TreeNode> que = new LinkedList<>(); que.offer(root);
while(!que.isEmpty()){ TreeNode node = que.poll(); if(node.val == val){ return node; }
if(node.left != null){ que.offer(node.left); }
if(node.right != null){ que.offer(node.right); } } return null; } }
|
700.二叉搜索树中的搜索(递归法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
class Solution { public TreeNode huisu(TreeNode root, int val){ if (root == null || root.val == val) { return root; }
if(val < root.val){ return huisu(root.left, val); }else{ return huisu(root.right, val); }
}
public TreeNode searchBST(TreeNode root, int val) { return huisu(root, val); } }
|
98.验证二叉搜索树(递归法实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { List<Integer> result = new ArrayList<>(); public void digui(TreeNode root){ if(root == null){ return; }
digui(root.left); result.add(root.val); digui(root.right); }
public boolean isValidBST(TreeNode root) { digui(root); for(int i = 1;i < result.size(); i ++){ if(result.get(i) <= result.get(i - 1)){ return false; } } return true; } }
|
98.验证二叉搜索树(迭代法实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
class Solution { public boolean isValidBST(TreeNode root) { Stack<TreeNode> stack = new Stack<>(); if(root != null){ stack.push(root); } TreeNode pre = null;
while(!stack.isEmpty()){ TreeNode curr = stack.peek(); if(curr != null){ stack.pop(); if(curr.right != null){ stack.push(curr.right); } stack.push(curr); stack.push(null); if(curr.left != null){ stack.push(curr.left); } }else{ stack.pop(); TreeNode tmp = stack.pop(); if(pre != null && pre.val >= tmp.val){ return false; } pre = tmp; } } return true; } }
|
98.验证二叉搜索树(递归,C++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| /** * 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, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: long long max_value = LONG_MIN; bool isValidBST(TreeNode* root) { if(root == NULL){ return true; }
bool left = isValidBST(root->left); if(max_value < root->val){ max_value = root->val; }else{ return false; } bool right = isValidBST(root->right); return left && right; } };
|
my-leetcode-logs-20230711
https://thewangyang.github.io/2023/07/11/leetcode-notes-20230711/