32. Binary Trees-2 (BFS/ DFS med problems)
Note-
1. Height of tree = no. of nodes in longest path = Max depth of a tree
2. Height of a node = 1 + max( LH, RH )
3. Path length of a path through a node = no. of edges in a path with this node as HCA
= no. of edges in left tree + in right sub tree
= Height of left sub tree + height of right sub tree
4. Diameter of a tree = max path length
5. Path sum = sum of all nodes in the path
-----------------------------------------------------
# Pattern to solve tree based problems:-
( e.g., for path sum => we need left & right path sum of a node => use L & R )
----------------------------------------------------------------------------------------------------------------------------
1. Max Depth of BT-
2. Balanced BT check-
it means for every node: [ H(left) - H(right) ] <=1
---------------------------------------------------------
Better- time: O(n)
modify the height code
each node returns:
height of sub tree, if balanced
and, -1 if unbalanced
3. Identical BT check-
-----------
e.g., recursive pre order DFS:
-----------------------------------------------------------------------------------------------------------------------------
4. Diameter(BT)-⭐
Diameter = no. of edges in longest path b/w any 2 nodes
This path may or may not pass through the root
------------------------------------------------------
Brute- time: O(n^2)
- for current node, find path length (= height[left sub tree] + height[right sub tree] )
- update answer variable
- recurse on children to repeat, until you reach null
(for nodes wali definition of diameter, take 1+lh+rh instead of lh+rh, while updating maxi)
Optimal- time: O(N)
modify height code (nodes based wala)
1. get left path length & right path length
2. update answer variable with total no. of edges in the current node's path
3. each child node will return max no. of edges associated to it in a path (i.e., max height)
-----------------------------------------------------------------------------------------------------------------------------
5. Max Path Sum-⭐
2. update answer variable with current node's path sum
3. each child node will return max path sum due to that node
2. if they are same, then just recurse on appropriate children of these nodes & check if they are same or not.
3. If both children return true, then trees are symmetric.
4. Base case means when either of the current node is null, then do a comparison b/w the 2 nodes
7. Zig Zag BFS-
1st level: L to R ; 2nd level : R to L ; and so on....
in BFS, for alternate levels, just reverse the elements in temp before storing in answer vector
8. Boundary Traversal-(ACW)
using multiple DFS traversals
----------------------------
- if current node is non leaf node, store in v1
----------------------------
2. Add all leaf nodes
- do any dfs traversal to store all the leaf nodes of the tree
------------------------------
3. Add right boundary nodes (from bottom to top)
- repeat these steps till you find a leaf node:
- store current node in v2
- if right exists => move to right
else => move to left
-------------------------------
9. Vertical order Traversal⭐
(BFS mapping)
for each vertical there are multiple unique levels
Approach- time: O(NlogN) space: O(n)
( we prefer BFS since we process nodes as per horizontal distance from root & also order inside a column matters & order needs level awareness )
-------------------------
1. we use BFS traversal to fill an ordered map
- map< int, map<int,multiset> >mpp : [col, (row, nodes)] = [ vertical, (level, node) ]
modified vertical traversal : in each vertical, store only first level nodes.
map and queue now do not store row & only stores col, since we want to store only first node in every column
only take last level nodes in each vertical











Comments
Post a Comment