Binary Tree - 81: Get Max Sum between two Leaves in Binary Tree

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Source Code:thecodingsimpl...
    Solution:
    - We start binary tree traversal in post order manner
    - We'll take a variable maxSum = 0
    - Now while traversing in post order, for each node, we'll store left + right + node value
    - If at any moment, if this value is greater than maxSum value then update the maxSum value
    Time Complexity: O(n)
    Space Complexity: O(1)
    Do Watch video for more info
    CHECK OUT CODING SIMPLIFIED
    / codingsimplified
    ★☆★ VIEW THE BLOG POST: ★☆★
    thecodingsimpli...
    I started my TH-cam channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 200+ videos.
    ★☆★ SUBSCRIBE TO ME ON TH-cam: ★☆★
    www.youtube.co...
    ★☆★ Send us mail at: ★☆★
    Email: thecodingsimplified@gmail.com

ความคิดเห็น • 35

  • @raghavddps2
    @raghavddps2 4 ปีที่แล้ว +3

    Your channel is gold!!❤❤, I will share it as much as possible. Lots of love!!

  • @letscodewithshivam
    @letscodewithshivam 2 ปีที่แล้ว +1

    very good explaination bhai

  • @rajverma5454
    @rajverma5454 4 ปีที่แล้ว +3

    Best explanation for this question. Thank you so much 😀.

    • @CodingSimplified
      @CodingSimplified  3 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

    • @abhayagarwal7753
      @abhayagarwal7753 3 ปีที่แล้ว

      Exactly gfg soln was killing my head man

  • @barshapal_1227
    @barshapal_1227 3 ปีที่แล้ว +1

    Truly loved the way of explaining

    • @CodingSimplified
      @CodingSimplified  3 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

  • @abhishekkumargupta3043
    @abhishekkumargupta3043 4 ปีที่แล้ว +6

    Best and complete explanation!! Well Done sir.
    Just one correction : maxSum should be INT_MIN instead of 0.

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว +1

      That's right. We should take this INT_MIN / Integer.Min_Value instead of 0. Thanks for notifying.

  • @amolmishra8621
    @amolmishra8621 4 ปีที่แล้ว +2

    I wonder why the views are so less. This video is very well explained!

    • @CodingSimplified
      @CodingSimplified  3 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 ปีที่แล้ว +2

    Very Nicely explained Sir !! Plzz make more videos on graph and Tree. Thanks for your effort in making this video.

  • @selvalooks
    @selvalooks 4 ปีที่แล้ว +1

    nicely explained..understandable!!!

  • @kunalpatidar2849
    @kunalpatidar2849 3 ปีที่แล้ว +2

    Sir when i am run this code in gfg it produce wrong output for some cases , can u please explain why it is ??

    • @nikhilpatro5905
      @nikhilpatro5905 3 ปีที่แล้ว +1

      www.geeksforgeeks.org/find-maximum-path-sum-two-leaves-binary-tree/
      There is a case of skewed tree which we need to handle separately. Even i was stuck on this testcase. Rest my logic was exactly same as this video said.

    • @CodingSimplified
      @CodingSimplified  3 ปีที่แล้ว

      Thanks for answering.

    • @JohnWick-kh7ow
      @JohnWick-kh7ow 3 ปีที่แล้ว

      @@nikhilpatro5905 So, What should we do if it it is asked in coding tests?

    • @nikhilpatro5905
      @nikhilpatro5905 3 ปีที่แล้ว

      @@JohnWick-kh7ow Check out the gfg article i have linked before. If the result is INT_MIN then return the returned value of the function. That will be the sum of all nodes from root to leaf in trees where all nodes have only 1 child.

    • @JohnWick-kh7ow
      @JohnWick-kh7ow 3 ปีที่แล้ว

      @@nikhilpatro5905 ok, I have understood. But I don't think I will be able to think of that case in tests.

  • @vanshchaudhary8291
    @vanshchaudhary8291 2 ปีที่แล้ว

    nice

  • @MohitMaroliyaBCS
    @MohitMaroliyaBCS 4 ปีที่แล้ว +1

    Sir your function is returning max(l,r)+1 everytime while ans of the problem is stored in maxSum which is never returned. For example discussed in video , when finally we reach to root node , we have l=10 and r = 12,so maxSUm = 10+12+2 = 24 which is correct but your functions return max(10,12)+2 = 14.

  • @soumavanag5025
    @soumavanag5025 3 ปีที่แล้ว

    Pure gold!! :)

    • @CodingSimplified
      @CodingSimplified  3 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

  • @SouravGhosh-yb1ee
    @SouravGhosh-yb1ee 4 ปีที่แล้ว +1

    just return INT_MIN....rather than going to further test cases

  • @Nisshant1112
    @Nisshant1112 4 ปีที่แล้ว +1

    Hi, what if there is only one single node : lets say [1], your variable maxSumBetweenTwoleaves will still be 0 but the answer should be 1

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      Thanks Nishant for notifying this. That's right. For this, at the starting of function, we can check, if there's only single node in tree, return this node value.

    • @kabirpeshawaria1703
      @kabirpeshawaria1703 3 ปีที่แล้ว

      ​@@CodingSimplified Thanks for this video! Actually @Nishant there is surprisingly no issue with having a single node. The algo shown here never updated maxSum when the current node has no children. So, the algo would correctly output maxSum = DEFAULT VALUE (either INT_MIN or NONE).
      Also, by the way, if there _were_ an issue with a single node, there would also be an issue with having the "tree" be one long branch, it would still only have one node with neither a non-null left pointer nor non-null right pointer.

  • @massvinod8413
    @massvinod8413 ปีที่แล้ว

    sir this code does not work for 5 N 6 -5 5 the answer should be 16 but it returns 6