333.Largest BST Subtree

    /*
     * 333.Largest BST Subtree
     * 2016-3-27 by Mingyang
     * 这个题目我的思路,自底向上的方法非常正确的!但是,这个题目独特的一点就在于对于一个
     * Tree是不是BST得判断,他必须表示对左子树的最大的还大,右子树的最小的还要小,所以这样看来就是
     * 必须要保证root必须保存当前子树1.isBST?2.left smallest.3.right biggest.4.node number
     * 可以先建一个class,也可以做一个array
     */
      public int largestBSTSubtree(TreeNode root) {
            if( root == null ) return 0;
            int [] ret = dfs1( root );
            return ret[3];
        }
        private int[] dfs1( TreeNode node ) {
            int[] l = new int[]{ 1, node.val, node.val, 0 }; //isBst, min, max, numNodesBST
            int[] r = new int[]{ 1, node.val, node.val, 0 };
            if( node.left != null ) l = dfs1 ( node.left );
            if( node.right != null ) r = dfs1( node.right );
            boolean isBst = l[0] == 1 && r[0] == 1 && node.val >= l[2] && node.val <= r[1];
            int numBstNodes = isBst ? 1 + l[3] + r[3] : Math.max( l[3], r[3] );
            return new int[]{ isBst ? 1 : 0, l[1], r[2], numBstNodes };
        }  
时间: 2024-12-18 02:28:05

333.Largest BST Subtree的相关文章

[LeetCode] Largest BST Subtree 最大的二分搜索子树

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descendants.Here's an example: 10 / 5 15 / \ \ 1 8 7 The Largest

LeetCode Largest BST Subtree

原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its desce

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

Leetcode总结之Tree

package Tree; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; class ListNode { int val; Lis

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】树(共94题)

[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *

[LeetCode] Count Univalue Subtrees 计数相同值子树的个数

Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest BST