zoukankan      html  css  js  c++  java
  • [LeetCode#98]Validate Binary Search Tree

    The problem: 

    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 greater than the node's key.
    • Both the left and right subtrees must also be binary search trees.

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


    OJ's Binary Tree Serialization:

    The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

    Here's an example:

       1
      / 
     2   3
        /
       4
        
         5
    
    The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
     
    My first solution(wrong): 
    public class Solution {
        public boolean isValidBST(TreeNode root) {
            if (root == null)
                return true;
    
            return helper(root);
        }
        
        private boolean helper(TreeNode temp_root) {
            
            if (temp_root == null)
                return true;
            
            if (temp_root.left != null) {
                if (temp_root.left.val >= temp_root.val)
                    return false;
            }
            
            if (temp_root.right != null) {
                if (temp_root.right.val <= temp_root.val)
                    return false;
            }
            
            return helper(temp_root.left) && helper(temp_root.right);
        }
        
    }

    Analysis:

    The recursion can only guarantee the comparsion between the temp_root with it's two direct children. 

    But the binary tree requries all left sub-tree's elements should less than temp_root, and all right sub-tree's elements should greater than temp_root.

    Consider the case: {10, 5, 15, #, #, 6, 10}

    The right way:

    The idea behind this problem is using the order properity of inorder traversal on binary search tree.
    Key: if a tree is a binary tree, the inorder traversal on it must lead to an order series(ascending).
    binary tree <=====>(inorder traversal) ordered array(ascending)

    Thus, by using inorder traversal on the tree, we could compare each element in the series array with its previous element. (This could be achieved by using an Arraylist) This is cool!!!

    At each element, we use following recursion rule:
    1. valid left & right sub-trees.
    2. check the current node's valid against its previous node. (inorder traversal)

    But, since we use inorder traversal, we could write it in following way:
    1. check left sub-tree's validation.
    2. compare the current node's against its previous node.
    3. check right sub-tree's validation.

    The right solution:

    public class Solution {
        public boolean isValidBST(TreeNode root) {
            if (root == null)
                return true;
    
            ArrayList<TreeNode> pre = new ArrayList<TreeNode> (); 
            pre.add(null);
    
            return helper(root, pre);
        }
        
        private boolean helper(TreeNode temp_root, ArrayList<TreeNode>pre) {
            
            if (temp_root == null)
                return true;
            
            boolean left_flag;
            boolean right_flag;
            
            left_flag = helper(temp_root.left, pre);
            
            if (pre.get(0) != null && pre.get(0).val >= temp_root.val) 
            //must be strict less than, otherwise it is invalid
                return false;
            pre.set(0, temp_root);
        
            right_flag = helper(temp_root.right, pre);
            
            return left_flag && right_flag; 
        }
    }
  • 相关阅读:
    2020-2021-2 网络对抗技术 20181318 Exp8 Web综合
    2020-2021-2 网络对抗技术 20181318 Exp7 网络欺诈防范
    2020-2021-2 网络对抗技术 20181318 Exp6 MSF应用基础
    实验一:密码引擎-加密API研究
    2020-2021-2 网络对抗技术 20181318 Exp5 信息搜集与漏洞扫描
    2020-2021-2 网络对抗技术 20181318 Exp4 恶意代码分析
    2020-2021-2 网络对抗技术 2018318 Exp3 免杀原理与实践
    2020-2021-2 网络对抗技术 20181318 Exp2 后门原理与实践
    2020-2021-2 网络对抗技术 20181318 Exp1 PC平台逆向破解
    makefile及其相关知识介绍
  • 原文地址:https://www.cnblogs.com/airwindow/p/4212157.html
Copyright © 2011-2022 走看看