LeetCode Binary Tree Pruning Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

This needs to check if root.left is null, root.right is null, and root.val is 0 to return null instead of [0]

ItsJbirdJustin
Автор

TreeNode* pruneTree(TreeNode* root) {
if(root == NULL)
return NULL;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(root->val == 0 && root->left == NULL && root->right == NULL)
root=NULL;
return root;
}

**This solution is quite easy to implement**

nero-kun-here
Автор

*Video Summary*
Question: Delete/prune a sub-tree if it doesn't contain a 1.
*Something not highlighted but important:*
You use POSTORDER tree traversal to traverse and check. "post" i.e. root node is traversed post exploration i.e. root node is explored in the end. Why? Because you want to check if a subtree's subtree has something or not (in this case a 1), only after which you can decide to prune or not, in which case you use postorder.

romirpatney
Автор

That girl speaking hindi in background 😘

QueenCoder
Автор

the solution fails for below input on leetcode.
[0, null, 0, 0, 0]

TechOnScreen
Автор

"Sorry bout the volume there's ppl talking we're in the LIBRARY" lol

jackedelic
Автор

class Solution {
public TreeNode pruneTree(TreeNode root) {
if(root == null) return null;
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if(root.left == null && root.right == null && root.val == 0){
return null;
}
return root;
}
}

amanmalhotra
Автор

on line number 13, the helper method returns a boolean value, even tho the value is not caught how it is complied successfully?

rupaldesai
Автор

I think most of the people in the library are Indians😂😂

sameer
Автор

If root==none:
Return
Fn(root.left)
Fn(root.right)
If root. Left==0 and root. Right==0 and root. Val==0:
Root==none


Why this is not working???

god-speed
Автор

TreeNode* pruneTree(TreeNode* root) {
if(root!=NULL){
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(!root->left && !root->right && root->val==0)
return NULL;
}
return root;
}

unseenworld
Автор

if(root==null) return null;


if(root.val!=1 && root.left==null && root.right==null) return null;
return root;

downey