From 208eaa6be988862683166e6e9f3fc1d5bc04a46b Mon Sep 17 00:00:00 2001 From: Dheeraj Kumar Date: Wed, 28 Oct 2020 09:34:36 +0530 Subject: [PATCH 1/3] c++ program for diameter of binary tree --- .../diameter_binary_tree.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp diff --git a/TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp b/TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp new file mode 100644 index 0000000..5b8e800 --- /dev/null +++ b/TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp @@ -0,0 +1,26 @@ + +// https://leetcode.com/problems/diameter-of-binary-tree/ + +class Solution { +public: + int d=0; + int height(TreeNode* curr_node) + { + if(curr_node ==NULL) + return 0; + int lh = height(curr_node ->left); + int rh = height(curr_node ->right); + + d = max(d , lh+rh); //calculate diameter across each node as root and update if it is max. + + return max(lh+1,rh+1); //return max height + + } + int diameterOfBinaryTree(TreeNode* root) + { + + height(root); + return d; + + } +}; \ No newline at end of file From f17cd3c0045e428df09594a70b4ee4bb2f20ab58 Mon Sep 17 00:00:00 2001 From: Dheeraj Kumar Date: Wed, 28 Oct 2020 09:45:57 +0530 Subject: [PATCH 2/3] c++ program for lowest common ancestor of binary tree --- .../lowest_common_ancestor_binary_tree.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 TreesAlgorithms/lowestCommonAncestorBinaryTree/lowest_common_ancestor_binary_tree.cpp diff --git a/TreesAlgorithms/lowestCommonAncestorBinaryTree/lowest_common_ancestor_binary_tree.cpp b/TreesAlgorithms/lowestCommonAncestorBinaryTree/lowest_common_ancestor_binary_tree.cpp new file mode 100644 index 0000000..cf36d56 --- /dev/null +++ b/TreesAlgorithms/lowestCommonAncestorBinaryTree/lowest_common_ancestor_binary_tree.cpp @@ -0,0 +1,29 @@ +// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ + + +class Solution { +public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) + { + if(root == NULL) + return NULL; + + if(root==p || root ==q) + return root; + TreeNode *l = lowestCommonAncestor(root->left, p, q); + TreeNode *r = lowestCommonAncestor(root->right, p, q); + + // If both of the above calls return Non-NULL, then one key + // is present in once subtree and other is present in other, + // So this node is the LCA + if(l && r) + return root; + + // Otherwise check if left subtree or right subtree is LCA + if(l) + return l; + else + return r; + + } +}; \ No newline at end of file From 48243685c5951e5482c2df47721e13deb0d4229b Mon Sep 17 00:00:00 2001 From: Dheeraj Kumar Date: Wed, 28 Oct 2020 09:59:59 +0530 Subject: [PATCH 3/3] c++ program for lowest common ancestor --- .../diameter_binary_tree.cpp | 26 ------------------- 1 file changed, 26 deletions(-) delete mode 100644 TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp diff --git a/TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp b/TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp deleted file mode 100644 index 5b8e800..0000000 --- a/TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp +++ /dev/null @@ -1,26 +0,0 @@ - -// https://leetcode.com/problems/diameter-of-binary-tree/ - -class Solution { -public: - int d=0; - int height(TreeNode* curr_node) - { - if(curr_node ==NULL) - return 0; - int lh = height(curr_node ->left); - int rh = height(curr_node ->right); - - d = max(d , lh+rh); //calculate diameter across each node as root and update if it is max. - - return max(lh+1,rh+1); //return max height - - } - int diameterOfBinaryTree(TreeNode* root) - { - - height(root); - return d; - - } -}; \ No newline at end of file