Jan 27 - Valid Binary Search Tree; DFS;

Using DFS to traverse the BST, when we look through the BST to a certain node, this node must have the following property to make sure the tree is a valid BST:

if current node is a left child of its parent, its value should smaller than its parent‘s value.

then its left child(if exists,), its value should smaller than its own. While its right child, if exists, the value should should be larger than current node‘s but smaller than the parent‘s

Similarly, we can find the property of a node which is a right child of its parent.

We use two long-type variable to record the MIN, MAX value in the remaining tree. When we goes into a left child of current, update the max to be current node‘s value, when we goes into a right child of current node, update the min to be current node‘s value. Basically the min and max value is related to the parent node‘s value. Initially we set  Min to be Integer.MIN_VALUE-1 and MAX to be Integer.MAX_VALUE +1, to make sure it works when we look into root‘s child nodes, here Using long data type to avoid over range of integer value.

Code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;
        long min = (long) (Integer.MIN_VALUE)-1;
        long max = (long) (Integer.MAX_VALUE)+1;
        return isValidLeftSubtree(root.left, root.val, min) && isValidRightSubtree(root.right, max, root.val);

    }
    public boolean isValidLeftSubtree(TreeNode node, long max, long min){
        if(node == null) return true;
        if(node.val >= max || node.val <= min) return false;
        return isValidLeftSubtree(node.left, node.val, min) && isValidRightSubtree(node.right, max, node.val);
    }

    public boolean isValidRightSubtree(TreeNode node, long max, long min){
        if(node == null) return true;
        if(node.val >= max || node.val <= min) return false;
        return isValidLeftSubtree(node.left, node.val, min) && isValidRightSubtree(node.right, max, node.val);
    }
}
时间: 2024-11-05 17:54:12

Jan 27 - Valid Binary Search Tree; DFS;的相关文章

Validate Binary Search Tree(DFS)

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

[LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Hide Tags Depth-first Search Linked List 这题是将链表变成二叉树,比较麻烦的遍历过程,因为链表的限制,所以深度搜索的顺序恰巧是链表的顺序,通过设置好递归函数的参数,可以在深度搜索时候便可以遍历了. TreeNode * help_f(Lis

LeetCode 98: Valid Binary Search Tree

This answer is so awesome!! /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { Integer min = null; public boolean isValidBST(TreeNode

Binary Search Tree DFS Template

Two methods: 1. Traverse 2. Divide & Conquer 1 // Traverse: usually do not have return value 2 public class Solution { 3 public void traverse(TreeNode root) { 4 if (root == null) 5 return; 6 traverse(root.left); 7 traverse(root.right); 8 } 9 } 10 11

leetcode dfs Validate Binary Search Tree

Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with

LeetCode: Validate Binary Search Tree 解题报告

Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node co

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o

LeetCode98 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). (Medium) Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes