Skip to content

Commit d6ec2cd

Browse files
committed
2017.03.12
1 parent 1d1269d commit d6ec2cd

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

BalancedBinaryTree.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool isBalanced(TreeNode* root) {
13+
if(root==NULL)
14+
return true;
15+
int left=height(root->left);
16+
int right = height(root->right);
17+
if(abs(left-right)>1)
18+
return false;
19+
else
20+
return isBalanced(root->left)&&isBalanced(root->right);
21+
}
22+
23+
public: int height(TreeNode* root)
24+
{
25+
if(root==NULL)
26+
return 0;
27+
int left=height(root->left);
28+
int right=height(root->right);
29+
return max(left,right)+1;
30+
}
31+
};

minDepthOfBinaryTree.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public int minDepth(TreeNode root) {
12+
if(root==null)
13+
{
14+
return 0;
15+
}
16+
if(root.left==null) return(minDepth(root.right)+1);
17+
if(root.right==null) return (minDepth(root.left)+1);
18+
return ( Math.min(minDepth(root.left),minDepth(root.right))+1);
19+
}
20+
}

pathSum.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public boolean hasPathSum(TreeNode root, int sum) {
12+
if(root==null)
13+
return false;
14+
if(root.left==null&&root.right==null&&sum-root.val==0)
15+
return true;
16+
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
17+
}
18+
}

pathSum3.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public int pathSum(TreeNode root, int sum) {
12+
if (root == null) return 0;
13+
return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
14+
}
15+
16+
private int pathSumFrom(TreeNode node, int sum) {
17+
if (node == null) return 0;
18+
return (node.val == sum ? 1 : 0)
19+
+ pathSumFrom(node.left, sum - node.val) + pathSumFrom(node.right, sum - node.val);
20+
}
21+
}
22+
/*
23+
if(root==null)
24+
return 0;
25+
return (pathSumFrom(root,sum)+pathSumFrom(root.left,sum)+pathSumFrom(root.right,sum));
26+
}
27+
public int pathSumFrom(TreeNode root, int sum)
28+
{
29+
if (root==null) return 0;
30+
return ((root.val==sum?1:0)+pathSumFrom(root.left,sum-root.val)+pathSumFrom(root.right,sum-root.val));
31+
32+
}
33+
*/
34+

0 commit comments

Comments
 (0)