Check if Binary tree is Sum tree or not | Love Babbar DSA Sheet | Amazon | Adobe 🔥 | GFG

  Рет қаралды 19,911

Yogesh & Shailesh (CodeLibrary)

Yogesh & Shailesh (CodeLibrary)

Күн бұрын

Пікірлер: 22
@coderaky
@coderaky 2 жыл бұрын
bool res=true; int f(Node* root){ if(!root)return 0; int l=f(root->left); int r=f(root->right); if(!root->left && !root->right)return root->data; if(root->data!=l+r)res=false; return l+r+root->data; } bool isSumTree(Node* root) { res=true; f(root); return res; }
@iztebah8118
@iztebah8118 2 жыл бұрын
We can return if (a+b != root->data). As we already found the solution no need to further iterate.
@dhruv2014
@dhruv2014 3 жыл бұрын
Nice code and easy to understand 🔥🔥
@divyanshuchaudhari5416
@divyanshuchaudhari5416 3 жыл бұрын
if (f==0):return 0 is not required.In every recursive call it is checking this condition even before node condition is checked.So,no use. Otherwise great explanation bro..Keep going.
@rajatmaheshwari4656
@rajatmaheshwari4656 3 жыл бұрын
also f = 1 in bool not req, just a global var is sufficient
@rogeliojaylon4477
@rogeliojaylon4477 3 жыл бұрын
So Nice 💘💘💘💘💘💘
@nirajdas1331
@nirajdas1331 3 жыл бұрын
+1 for elegant solution
@hiteshusingh8571
@hiteshusingh8571 3 жыл бұрын
Good Explanation!
@sandipan_c
@sandipan_c Жыл бұрын
There's no need to continue further if we see that, sum of leaves and its root is not equal. Python code is attached below. def recur(node): nonlocal flag if node is None: return 0 if node.left is None and node.right is None: return node.data else: sl = recur(node.left) sr = recur(node.right) if sl == -1 or sr == -1: return -1 if sl+sr != node.data: return -1 return sl + sr + node.data p = recur(root) return 0 if p == -1 else 1
@kunalverma9777
@kunalverma9777 3 жыл бұрын
brute->better->optimal only discussing code is not helpful at all
@akashchanabasanavar3031
@akashchanabasanavar3031 Жыл бұрын
Is this code pass the case???
@ankur420ify
@ankur420ify Жыл бұрын
solution not working for all corner cases.
@sainisaab684
@sainisaab684 2 жыл бұрын
how to detrermine stack of tree is used or not!!???
@BACSShaileshShettar
@BACSShaileshShettar 4 ай бұрын
the code provided in video is wrong. below is correct code. class Solution { boolean isSumTree(Node root) { boolean[] ans=new boolean[1]; ans[0]=true; int res=recurse(root,ans); return ans[0]; } static int recurse(Node root,boolean[] ans) { if (root==null) return 0; int left=recurse(root.left,ans); int right=recurse(root.right,ans); int sum=left+right; if (sum!=root.data && sum!=0) ans[0]=false; root.data+=sum; //System.out.println(root.data); return root.data; } }
@mastersk7440
@mastersk7440 3 жыл бұрын
Sare topic ka basic ka bad question karwaya
@aishwaryajaiswal4720
@aishwaryajaiswal4720 3 жыл бұрын
I wrote the same code and it's not working
@akshaytak7775
@akshaytak7775 2 жыл бұрын
Maybe you globally initialized f =1, due to which your function is always returning true.
@Viralmems_xyz
@Viralmems_xyz Жыл бұрын
bakbass explain
@mastersk7440
@mastersk7440 3 жыл бұрын
Bhai basic se karwoa yar
@luffyy8084
@luffyy8084 3 жыл бұрын
62 16 15 N 8 4 7 N 8 4 for this input gfg is saying output is wrong..can anyone help why this code doesnt work for this input?
@akashjaiswal6478
@akashjaiswal6478 3 жыл бұрын
check after sometime it will give correct solution
@samarthbhasin1204
@samarthbhasin1204 2 жыл бұрын
yess
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
L29. Children Sum Property in Binary Tree | O(N) Approach | C++ | Java
16:13
Print all "K" Sum paths in a Binary tree |  | Love Babbar DSA Sheet | GFG 🔥 | Amazon
12:45
Yogesh & Shailesh (CodeLibrary)
Рет қаралды 19 М.
NVIDIA’s New AI: Stunning Voice Generator!
6:21
Two Minute Papers
Рет қаралды 83 М.
L15. Check for Balanced Binary Tree | C++ | Java
12:30
take U forward
Рет қаралды 359 М.
Check for Binary Search Tree | C++ Placement Course | Lecture 28.4
13:46