Given a BT and a key Node, find the total sum in BT, except those Node which are adjacent to key Node.
1. Traverse the tree using pre-order.
2. If current node is adjacent to the key then do not add it to the final sum.
3. If current node is the key then do not add it’s children to the final sum.
4. If key is not present then return sum of all nodes.
Time Complexity : O(n) where n is number of nodes in the BT.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- K-th ancestor of a node in Binary Tree | Set 3
- K-th ancestor of a node in Binary Tree
- Search a node in Binary Tree
- Sum of cousins of a given node in a Binary Tree
- Get Level of a node in a Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- Replace node with depth in a binary tree
- Get maximum left node in binary tree
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Preorder predecessor of a Node in Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Get last node of the binary tree following given pattern starting from X
- Postorder successor of a Node in Binary Tree
- Preorder Successor of a Node in Binary Tree
- Find mirror of a given node in Binary tree
- Print cousins of a given node in Binary Tree
- Closest leaf to a given node in Binary Tree
- Find the Deepest Node in a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Rajput-Ji