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 a5bb49f

Browse files
committed
Python solutions
1 parent 6110755 commit a5bb49f

File tree

4 files changed

+157
-4
lines changed

4 files changed

+157
-4
lines changed

‎README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
| :------------: | :----------: |
99
| Total Problems | 188 |
1010

11+
Current Daily Streak: 1 (started on 06/20/2019)
12+
1113
</center>
1214

1315
**Note: Some of the code here is old and was written when I was learning C++. It might be possible that code is not safe or making wrong assumptions. Please use with caution. Pull requests are always welcome.**
@@ -119,7 +121,7 @@ Include contains single header implementation of data structures and some algori
119121
|Given a binary tree (unlike binary search tree), find the Lowest Common Ancestor (LCA).|[lowest-common-ancestor-binary-tree.cpp](tree_problems/lowest-common-ancestor-binary-tree.cpp)|
120122
|Given a binary tree, print out all of its root-to-leaf paths one per line.| [printAllRootToLeafPath.cpp](tree_problems/printAllRootToLeafPath.cpp)
121123
|Determine if a tree is sum tree. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.| [sumTree.cpp](tree_problems/sumTree.cpp)|
122-
|Convert a tree to sumTree, such that each node is sum of left and right subtree of the original tree.| [convert_to_sum_tree.cpp](tree_problems/convert_to_sum_tree.cpp)|
124+
|Convert a tree to sumTree, such that each node is sum of left and right subtree of the original tree.| [convert_to_sum_tree.cpp](tree_problems/convert_to_sum_tree.cpp), [convert_to_sum_tree.py](tree_problems/convert_to_sum_tree.py)|
123125
| Convert a sorted array to balanced binary search tree.| [sortedArrayToBST.cpp](tree_problems/sortedArrayToBST.cpp)|
124126
| Given a binary tree, generate sum of each vertical column.|[verticalSum.cpp](tree_problems/verticalSum.cpp)|
125127
| Given a binary tree and key, node with key exists in tree. Find all the ancestors of the node with key, ancestor here are the nodes which are in straight path from node to root.| [node_ancestors_in_root_path.cpp](tree_problems/node_ancestors_in_root_path.cpp)|
@@ -131,7 +133,7 @@ Include contains single header implementation of data structures and some algori
131133
| Find kth smallest element in a binary search tree | [kth_smallest.cpp](tree_problems/kth_smallest.cpp)|
132134
| Validate if a given binary tree is a binary search tree. | [validate_bst.cpp](tree_problems/validate_bst.cpp) |
133135
| Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.| [find_target_k.cpp](tree_problems/find_target_k.cpp) |
134-
| Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Also, to note that the target value is a floating point. There will be only one unique value which is closest to the target. |[closest_bst_value.cpp](tree_problems/closest_bst_value.cpp) |
136+
| Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Also, to note that the target value is a floating point. There will be only one unique value which is closest to the target. |[closest_bst_value.cpp](tree_problems/closest_bst_value.cpp), [closest_bst_value.py](tree_problems/closest_bst_value.py) |
135137
| Given a binary tree, traversing preorder, construct a string output containing node values and parenthesis. The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree. Examples in code file| [string_from_tree.cpp](tree_problems/string_from_tree.cpp)|
136138

137139
### String Problems

‎tree_problems/closest_bst_value.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ int main()
6767
std::cout << "Inorder traversal of tree: ";
6868
print_inorder(root);
6969
std::cout << std::endl;
70-
std::cout << "Closest value from 8.6778 is :" << closest_bst_value(root, 3.6778)
70+
std::cout << "Closest value from 3.6778 is :" << closest_bst_value(root, 3.6778)
7171
<< std::endl;
7272

73-
std::cout << "(Iterative) Closest value from 8.6778 is :"
73+
std::cout << "(Iterative) Closest value from 3.6778 is :"
7474
<< closest_bst_value_iterative(root, 3.6778) << std::endl;
7575
return 0;
7676
}

‎tree_problems/closest_bst_value.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'''
2+
Given a non-empty binary search tree and a target value,
3+
find the value in the BST that is closest to the target.
4+
Also, to note that the target value is a floating point.
5+
There will be only one unique value which is closest to the target.
6+
'''
7+
8+
import sys
9+
10+
11+
class TreeNode:
12+
'''
13+
A tree's node representation
14+
'''
15+
def __init__(self, data):
16+
self.data = data
17+
self.left = None
18+
self.right = None
19+
20+
def closest_diff_util(node, target, min_diff, min_diff_data):
21+
''' Util function to find the node closest to target
22+
Params:
23+
node -- node in the current BST tree
24+
target -- The value to which closest node needs to be found.
25+
min_diff -- minimum difference between target and the nodes we have iterated so far
26+
min_diff_data -- the value of node which is closest to target so far.
27+
'''
28+
if node == None:
29+
return
30+
31+
# If the target itself is present in the tree.
32+
if node.data == target:
33+
min_diff_data[0] = target
34+
return
35+
36+
if min_diff > abs(node.data - target):
37+
min_diff = abs(node.data - target)
38+
min_diff_data[0] = node.data
39+
40+
if target < node.data:
41+
closest_diff_util(node.left, target, min_diff, min_diff_data)
42+
else:
43+
closest_diff_util(node.right, target, min_diff, min_diff_data)
44+
45+
def closest_diff(root, target):
46+
'''Function to find the node closest to target
47+
Params:
48+
root -- the root node of the tree
49+
target -- the value to which closest node needs to be found.
50+
'''
51+
min_diff, min_diff_data = sys.maxsize, [-1]
52+
closest_diff_util(root, target, min_diff, min_diff_data)
53+
return min_diff_data[0]
54+
55+
def closest_diff_iter(root, target):
56+
'''Function to find the node closest to target iteratively
57+
Params:
58+
root -- the root node of the tree
59+
target -- the value to which closest node needs to be found.
60+
'''
61+
if not root:
62+
return sys.maxsize
63+
closest = root.data
64+
while root:
65+
if abs(target - closest) >= abs(target - root.data):
66+
closest = root.data
67+
root = root.left if target < root.data else root.right
68+
return closest
69+
70+
def inorder(root):
71+
"""Print inorder traversal of the tree.
72+
Param:
73+
root -- root of the tree.
74+
"""
75+
if not root:
76+
return
77+
inorder(root.left)
78+
print(root.data, end=" ")
79+
inorder(root.right)
80+
81+
if __name__ == '__main__':
82+
root = TreeNode(10);
83+
root.left = TreeNode(5);
84+
root.right = TreeNode(15);
85+
root.left.left = TreeNode(2);
86+
root.left.right = TreeNode(7);
87+
root.right.left = TreeNode(12);
88+
root.right.right = TreeNode(16);
89+
90+
print("Inorder traversal of the tree:")
91+
inorder(root)
92+
93+
print()
94+
print("Closest value in the tree (recursively) to 6.6779 : ", closest_diff(root, 6.6779))
95+
print("Closest value in the tree (iteratively) to 6.6779 : ", closest_diff_iter(root, 6.6779))

‎tree_problems/convert_to_sum_tree.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Convert a given tree to to sum tree.
3+
Given : A tree with positive and negative data values.
4+
Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree.
5+
The values of leaf nodes are changed to 0.
6+
10 20
7+
/ \ / \
8+
-2 6 ----> 4 12
9+
/ \ / \ / \ / \
10+
8 -4 7 5 0 0 0 0
11+
"""
12+
13+
class TreeNode:
14+
"""Representation of a binary tree node"""
15+
def __init__(self, data):
16+
self.data = data
17+
self.left = None
18+
self.right = None
19+
20+
def to_sum_tree(root):
21+
"""Converts a given binary tree to a sum tree
22+
params:
23+
root: Root of the binary tree
24+
"""
25+
if not root:
26+
return 0
27+
28+
previous_val = root.data
29+
root.data = to_sum_tree(root.left) + to_sum_tree(root.right)
30+
return root.data + previous_val
31+
32+
def inorder(root):
33+
"""Prints inorder traversal of the binary tree
34+
params:
35+
root: Root of the binary tree
36+
"""
37+
if not root:
38+
return
39+
inorder(root.left)
40+
print(root.data, end=' ')
41+
inorder(root.right)
42+
43+
44+
if __name__ == '__main__':
45+
root = TreeNode(10)
46+
root.left = TreeNode(-2)
47+
root.right = TreeNode(6)
48+
root.left.left = TreeNode(8)
49+
root.left.right = TreeNode(-4)
50+
root.right.left = TreeNode(7)
51+
root.right.right = TreeNode(5)
52+
print("Inorder traversal of the tree: ")
53+
inorder(root)
54+
to_sum_tree(root);
55+
print("\nInorder traversal of the sum tree: ")
56+
inorder(root)

0 commit comments

Comments
(0)

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