Validate Binary Search Tree

Поделиться
HTML-код
  • Опубликовано: 9 ноя 2024

Комментарии • 77

  • @KevinNaughtonJr
    @KevinNaughtonJr  4 года назад +4

    instagram: instagram.com/kevinnaughtonjr/
    twitter: instagram.com/kevinnaughtonjr/

  • @lauratran7966
    @lauratran7966 3 года назад +7

    I currently moved past a few phone interviews and I wanted to say thank you for all the help from your videos! I appreciate the quick and concise explanations.

  • @bhaskar_unplugged
    @bhaskar_unplugged 4 года назад +47

    There are 2 more ways to solve it.
    1. Do inorder traversal. It traverses the tree in sorted order. If it is not in sorted order, it is not valid bst
    2. Bottom up. The current node, Left subtree and right subtree of each node should be valid bst

    • @avinash9307
      @avinash9307 4 года назад +5

      I don't think the first method works for inputs like [1,1] where the inorder traversal and the sorted list are both equal and yet it is not a valid BST.

    • @Bargains20xx
      @Bargains20xx 4 года назад +1

      @@avinash9307 yup, i was going to say that, most places that mention that approach, mention it with caution

    • @Deamer24
      @Deamer24 4 года назад +1

      Avinash Parasurampuram You can make approach one work for cases like [1,1] by checking for duplicates as well as the sorted ordering since duplicates are next to each other in the traversal order

    • @josephs.7960
      @josephs.7960 4 года назад +1

      @@avinash9307 simply check that list[i] is greater than list[i-1] for i from 1 to length of list -1

  • @kevincarr2334
    @kevincarr2334 4 года назад +7

    Kevin great audio quality, thanks for making a clear and easy to understand video!

  • @georgiossamaras5063
    @georgiossamaras5063 3 года назад +4

    In C++ you would need to use `long` as the type of `min` and `max` in order to pass all Leetcode's test cases (so use `LONG_MIN` and `LONG_MAX`).

  • @omarsherif6198
    @omarsherif6198 3 года назад +1

    Your voice and solutions are smart. But I would recommend if you can put an example or a walkthrough your code with some other visuals so it's easier to understand.
    Anyways thank you man, love your channel.

  • @consciousmi4842
    @consciousmi4842 4 года назад +1

    Nice content. Thanks for uploading.

  • @abhikbhattacharya1130
    @abhikbhattacharya1130 4 года назад

    Great explanation dude. Your videos are concise and your code is great. Keep up the good work🤘.

  • @KEVINMBARUA
    @KEVINMBARUA 4 года назад +15

    Kevin if I ace this Facebook onsite interview, I owe you a coffee bro! thanks

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад +4

      Kevin Murimi looking forward to that coffee :) best of luck!!! If you want additional help preparing check out my Patreon page www.patreon.com/KevinNaughtonJr

    • @fionamatu4996
      @fionamatu4996 4 года назад +11

      How did your interview go @Kevin?

    • @mehmetfrataksoy4009
      @mehmetfrataksoy4009 3 года назад

      how was the interview bro

    • @KEVINMBARUA
      @KEVINMBARUA 3 года назад +13

      @@fionamatu4996 @Mehmet Firat I didn't land the gig coz I bottled the System Design Round. I interviewed at Twitter, Microsoft and some UK startup a week later and landed all three though.

    • @KEVINMBARUA
      @KEVINMBARUA 3 года назад +4

      @@KevinNaughtonJr how can I buy you the coffee bro? I didn't land the Facebook one but I landed both Microsoft and Twitter :)

  • @arrahul316
    @arrahul316 2 года назад

    Thanks Kevin.

  • @carlosluque4753
    @carlosluque4753 Год назад

    Hi Kevin! fantastic video, it's helping me a lot! Could you help me understand why the time complexity is O(N) in lieu of O(2^n)? I am failing to see why. Thanks

  • @madpotatoo
    @madpotatoo 4 года назад +3

    great solution! (this question really doesn't feel like a medium one, more of an easy in my opinion

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад

      Thanks!

    • @miteshkumar5557
      @miteshkumar5557 4 года назад +1

      It is a medium because in an interview they would only accept the in order traversal method. The other two methods are very verbose and messy.

    • @FINSuojeluskunta
      @FINSuojeluskunta 3 года назад +2

      It seems easy because you're being explained the method in a video as opposed to figuring it out yourself. There are a lot of nuisances with this one IMO

  • @KILLERDOG416
    @KILLERDOG416 4 года назад +1

    this is a great series and pls make more of these
    and im also at nyu!!

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад

      thanks and NO WAY go violets/bobcats/whatever NYU is!!!!

    • @Makwayne
      @Makwayne 2 года назад

      Also from NYU here

  • @arijitroy8390
    @arijitroy8390 3 года назад +1

    Why INTEGER is used in place of int?

  • @surajgrandhi6742
    @surajgrandhi6742 4 года назад

    Amazing explanation brother

  • @armanhosyan5660
    @armanhosyan5660 4 года назад +1

    Awesome!!!

  • @ankitsharma-gq3yv
    @ankitsharma-gq3yv 4 года назад

    Very well explained. Could you please post video for buy/sell stock problem I, II and III?

  • @algorithmimplementer415
    @algorithmimplementer415 4 года назад

    Skew tree is the word.

  • @joineynguyen
    @joineynguyen 4 года назад

    Well explained. Thank you

  • @abhisheksuryavanshi9675
    @abhisheksuryavanshi9675 3 года назад +4

    Doesn't work for->[0,null,-1]

  • @poojaguru2516
    @poojaguru2516 4 года назад +1

    Awesome🔥

  • @mr_phamtastic
    @mr_phamtastic 3 года назад

    awesome explanation!

  • @nimanizky
    @nimanizky 4 года назад +1

    I hear brockhampton, i press like

  • @luomin6357
    @luomin6357 4 года назад

    I like your solution, much better than mine.

  • @ph4z0n18
    @ph4z0n18 4 года назад +2

    Thanks for these vids! They're super helpful. Do you have plans to do some of the harder leetcode questions sometime?

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад

      Anytime! And I’d be happy to take suggestions for other problems you want done if you have any!

    • @ph4z0n18
      @ph4z0n18 4 года назад

      @@KevinNaughtonJr Can you do some questions involving heaps?

  • @hacker-7214
    @hacker-7214 4 года назад +1

    Yaaay youre back~!

  • @shawnswanson7721
    @shawnswanson7721 3 года назад

    What about for [0,null,-1]?

  • @vijayselvam5130
    @vijayselvam5130 4 года назад

    Hi Kevin, I'm a fan of your channel. Thanks a lot for all your quick videos. But I believe the below case is not covered here.
    ```Tree tree1 = new Tree(6);
    tree.left = new Tree(2);
    tree.right = new Tree(8);
    tree.left.left = new Tree(1);
    tree.left.right = new Tree(9);```

  • @Ziggy0120
    @Ziggy0120 3 года назад

    Hey Kevin. I'm a little confused on how the root being null would return true. wouldn't the root being null make the tree false?

  • @thedevsaddam
    @thedevsaddam 4 года назад +1

    Awesome (Y)

  • @vk1618
    @vk1618 4 года назад

    Look for all cases, forgot to valisate for max

  • @darod6098
    @darod6098 4 года назад +3

    I don't know why you put (root, max, min) instead of (root, min, max). it is confusing. anyway, thank you!

    • @rodanm
      @rodanm 3 года назад

      Because it is common to check for the left node and then the right node. In this case, max represented the max of the left so it goes first. Also thought about it.

  • @JamesHalpert8555
    @JamesHalpert8555 4 года назад +3

    This is a smart solution!!!☺👍👍but how about an inorder traversal that would store all the elements of a BST in sorted order?? if it's not strictly in sorted order return false.

  • @tsupreetsingh
    @tsupreetsingh 4 года назад

    Can you make a playlist of all the songs you have used in your intro ?

  • @ashokbalakrishnan1371
    @ashokbalakrishnan1371 4 года назад

    Nice video Kevin. Do we solve this using any loop without recursion?

    • @Ziggy0120
      @Ziggy0120 3 года назад

      you would use a depth first search or a breadth first search to validate the tree iteratively. you would more or less use the same conditional ideas as in the recursion solution but you would need to use it with a Queue or Stack data structure depending on which search you use.

  • @rakeshaggarwal7616
    @rakeshaggarwal7616 2 года назад

    Below scenario is not covered
    /* 100
    50 150
    25 75 125 175*/

  • @krishnasivakumar2479
    @krishnasivakumar2479 4 года назад +1

    DFS is the way to go

  • @jak0495
    @jak0495 4 года назад

    two videos in two days? someone stop this man

  • @MiguelMartinez-yj7yu
    @MiguelMartinez-yj7yu 3 года назад

    holy crap it took me so long to understand the last part. embarrassing

  • @ipman92
    @ipman92 2 года назад +1

    Why can't we just do recursion inside of the original function instead of creating a helper function? Can't we just update the max to be root.right.val and min to be root.left.val every time?

  • @Salesforce_Nexus
    @Salesforce_Nexus 4 года назад

    pls try to explain with example, i didn't get the last one