File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ # Python3 Bottom View Binary Tree
2+
3+ class Node :
4+ def __init__ (self , hd , data ):
5+ self .left = None
6+ self .right = None
7+ self .hd = hd
8+ self .data = data
9+
10+ def BottomViewTree (root ):
11+ if not root :
12+ return
13+ q = []
14+ m = {}
15+ hd = 0
16+ root .hd = hd
17+ q .append (root )
18+ while q :
19+ hd = root .hd
20+ m [hd ] = root .data
21+ if root .left :
22+ root .left = hd - 1
23+ q .append (root .left )
24+ if root .right :
25+ root .right = hd + 1
26+ q .append (root .right )
27+ q .pop (0 )
28+ root = q [0 ]
29+
30+ print (* m .values ())
Original file line number Diff line number Diff line change 1+ # Python3 Top View of Binary Tree
2+
3+ class Node :
4+ def __init__ (self , data , hd ):
5+ self .left = None
6+ self .right = None
7+ self .hd = hd
8+ self .data = data
9+
10+ def TopViewTree (root ):
11+ if root == None :
12+ return
13+ q = []
14+ m = {}
15+ hd = 0
16+ root .hd = hd
17+ q .append (root )
18+ while q :
19+ hd = root .hd
20+ if hd not in m :
21+ m [hd ] = root .data
22+ if root .left :
23+ root .left = hd - 1
24+ q .append (root .left )
25+ if root .right :
26+ root ,right = hd + 1
27+ q .append (root .right )
28+ q .pop (0 )
29+ root = q [0 ]
30+ print (* m .values ())
You can’t perform that action at this time.
0 commit comments