您的位置:首页 > 产品设计 > UI/UE

Leetcode 95. Unique Binary Search Trees II及二叉树最大最小深度镜像树总结

2017-05-14 22:43 591 查看

95. Unique Binary Search Trees II

DescriptionHintsSubmissionsSolutions

Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.

For example,

Given n = 3, your program should return all 5 unique BST’s shown below.

1         3     3      2      1
\       /     /      / \      \
3     2     1      1   3      2
/     /       \                 \
2     1         2                 3


import java.util.ArrayList; import java.util.List;

/* Created by happy on 17/5/14. */ public class

UniqueBinarySearchTreesII {

public List generateTrees(int n) {

if(n==0) return new ArrayList();

return genTrees(1,n);

}

public List<TreeNode> genTrees (int start, int end){
List<TreeNode> res = new ArrayList<TreeNode>();
if(start>end) {
res.add(null);
return res;
}
if(start==end){
res.add(new TreeNode(start)) ;
return res;
}
List<TreeNode> left,right;
for(int i=start;i<=end;i++)
{
left = genTrees(start,i-1);
right = genTrees(i+1,end);
for(TreeNode lnode:left){
for(TreeNode rnode:right){
TreeNode root = new TreeNode(i);
root.left = lnode;
root.right = rnode;
res.add(root);

}
}

}
return res;
} }


-

100.Same Tree

Description

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null) return true;
if(p==null||q==null) return false;
if(p.val!=q.val) return false;
return isSameTree(p.left,q.left)&& isSameTree(p.right,q.right);
}


101. Symmetric Tree

Description

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

1
/ \
2   2
/ \ / \
3  4 4  3


But the following [1,2,2,null,3,null,3] is not:

1
/ \
2   2
\   \
3    3


Note:

Bonus points if you could solve it both recursively and iteratively.

/**
* Created by happy on 17/5/14.
*/
public class SymmetricTree {
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return helper(root.left,root.right);
}

public boolean helper(TreeNode left,TreeNode right){
if(left==null&&right==null) return true;
if(left==null||right==null) return false;
if(left.val!=right.val) return false;
return helper(left.left,right.right)&&helper(left.right,right.left);
}
}


111. Minimum Depth of Binary Tree

Description

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

/**
* Created by happy on 17/5/14.
*/
public class MinimumDepthOfBinaryTree {
public int minDepth(TreeNode root) {
if(root==null) return 0;
int left = minDepth(root.left)+1;
int right = minDepth(root.right)+1;
return (left == 0 || right == 0) ?
left + right + 1: Math.min(left,right) + 1;    }
//或 Math.max(left,right) + 1: Math.min(left,right) + 1;    }
}


104. Maximum Depth of Binary Tree

Description

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

/**
* Created by happy on 17/3/22.
*/
public class MaximumDepthOfBinaryTree {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
if(root.left==null && root.right==null) return 1;
int left = maxDepth(root.left)+1;
int right = maxDepth(root.right)+1;
return left>=right?left:right;
}
}


124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tr
4000
ee along the parent-child connections. The path must contain at least one node and does not need to go through the root.

For example:

Given the below binary tree,

1
/ \
2   3


Return 6.

import java.util.ArrayList;

/**
* Created by happy on 17/4/15.
* 任意一点到另一点的路径之和的最大值
*  * 需要考虑节点值为负数
*/
public class BinaryTreeMaximumPathSum {
int res =Integer.MIN_VALUE;

public int maxPathSum(TreeNode root) {
helper(root);
return res;
}

public int helper(TreeNode node){
if(node==null) return 0;
int left = Math.max(0,helper(node.left));
int right = Math.max(0,helper(node.right));
int cur = node.val+left+right; //(left>0?left:0)+(right>0?right:0);
if(cur>res) res=cur;
return Math.max(left, right)+node.val;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 二叉树