Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78de61e

Browse files
Update Interview_Questions.md
1 parent 559fb9a commit 78de61e

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

‎Interview_Questions.md‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,94 @@ Solution: There are many types of sorting algorithms: quick sort, bubble sort, b
3838

3939
## Some common algorithm problems asked at online tests :
4040

41-
#### 10. Save all leaf nodes of a Binary tree in a Doubly Linked List by using Right node as Next node and Left Node as Previous Node.
41+
## 10. Save all leaf nodes of a Binary tree in a Doubly Linked List by using Right node as Next node and Left Node as Previous Node.
42+
43+
```
44+
# Python program to extract leaf nodes from binary tree
45+
# using double linked list
46+
47+
# A binary tree node
48+
class Node:
49+
50+
# Constructor to create a new node
51+
def __init__(self, data):
52+
self.data = data
53+
self.left = None
54+
self.right = None
55+
56+
# Main function which extracts all leaves from given Binary Tree.
57+
# The function returns new root of Binary Tree (Note that
58+
# root may change if Binary Tree has only one node).
59+
# The function also sets *head_ref as head of doubly linked list.
60+
# left pointer of tree is used as prev in DLL
61+
# and right pointer is used as next
62+
def extractLeafList(root):
63+
64+
# Base Case
65+
if root is None:
66+
return None
67+
68+
if root.left is None and root.right is None:
69+
# This node is going to be added to doubly linked
70+
# list of leaves, set pointer of this node as
71+
# previous head of DLL. We don't need to set left
72+
# pointer as left is already None
73+
root.right = extractLeafList.head
74+
75+
# Change the left pointer of previous head
76+
if extractLeafList.head is not None:
77+
extractLeafList.head.left = root
78+
79+
# Change head of linked list
80+
extractLeafList.head = root
81+
82+
return None # Return new root
83+
84+
# Recur for right and left subtrees
85+
root.right = extractLeafList(root.right)
86+
root.left = extractLeafList(root.left)
87+
88+
return root
89+
90+
# Utility function for printing tree in InOrder
91+
def printInorder(root):
92+
if root is not None:
93+
printInorder(root.left)
94+
print root.data,
95+
printInorder(root.right)
96+
97+
98+
def printList(head):
99+
while(head):
100+
if head.data is not None:
101+
print head.data,
102+
head = head.right
103+
104+
# Driver program to test above function
105+
extractLeafList.head = Node(None)
106+
root = Node(1)
107+
root.left = Node(2)
108+
root.right = Node(3)
109+
root.left.left = Node(4)
110+
root.left.right = Node(5)
111+
root.right.right = Node(6)
112+
root.left.left.left = Node(7)
113+
root.left.left.right = Node(8)
114+
root.right.right.left = Node(9)
115+
root.right.right.right = Node(10)
116+
117+
print "Inorder traversal of given tree is:"
118+
printInorder(root)
119+
120+
root = extractLeafList(root)
121+
122+
print "\nExtract Double Linked List is:"
123+
printList(extractLeafList.head)
124+
125+
print "\nInorder traversal of modified tree is:"
126+
printInorder(root)
127+
128+
```
42129

43130
#### 11. Given an array,find the maximum j – i such that arr[j] > arr[i]
44131

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /