第一题 求二叉树最大深度
遍历方法:
分治方法:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer
*/
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int dep = Math.max(maxDepth(root.left),maxDepth(root.right));
return dep+1;
}
}
第二题 判断平衡二叉树
思路:如果直接调用上面的函数不停地比较高度那么效率太低了,因为要重复计算很多次。所以我们可以设置一个全局变量,这样的话只需要遍历一次就可以得到结果。
代码:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
boolean isBalance = true;
public boolean isBalanced(TreeNode root) {
// write your code here
depth(root);
return isBalance;
}
public int depth(TreeNode root){
if(root==null)
return 0;
int left = depth(root.left);
int right = depth(root.right);
if(Math.abs(left-right)>1){
isBalance = false;
}
return Math.max(left,right)+1;
}
}
第三题:二叉树最大路径
情况1:如果是从头到叶子结束
public int maxPathSum(TreeNode node){
if(node==null){
return 0;}
return Math.max(maxPathSum(node.left),maxPathSum(node.right))+node.val;
}
情况2:如果是从头到任意节点结束
public int maxPathSum(TreeNode node){
if(node==null){
return 0;}
int max = Math.max(maxPathSum(node.left),maxPathSum(node.right));
return max>0?max:0+node.val;
}
情况3:any to any
二叉树任意节点到任意节点
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer
*/
public int maxPathSum(TreeNode root) {
if(root==null)
return Integer.MIN_VALUE;
return anyToAny(root);
}
public int anyToAny(TreeNode node){
if(node==null)
return Integer.MIN_VALUE;
int left = anyToAny(node.left);
int right = anyToAny(node.right);
int anyToAnyMax = Math.max(left,right);
return Math.max(anyToAnyMax,Math.max(rootToAny(node.left),0)+Math.max(rootToAny(node.right),0)+node.val);
}
public int rootToAny(TreeNode node){
if(node==null)
return Integer.MIN_VALUE;
return Math.max(0,Math.max(rootToAny(node.left),rootToAny(node.right)))+node.val;
}
}
第四题:binary-search-tree-iterator
代码:
public class BSTIterator {
private Stack<TreeNode> stack = new Stack<>();
private TreeNode root;
/*
* @param root: The root of binary tree.
*/public BSTIterator(TreeNode root) {
// do intialization if necessary
this.root = root;
}
/*
* @return: True if there has next node, or false
*/
public boolean hasNext() {
// write your code here
return !(root==null&&stack.isEmpty());
}
/*
* @return: return next node
*/
public TreeNode next() {
// write your code here
while(root!=null){
stack.push(root);
root= root.left;
}
root=stack.pop();
TreeNode node = root;
root = root.right;
return node;
}
}
第五题、binary-tree-level-order-traversal
public class Solution {
/**
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if(root==null)
return list;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while(!que.isEmpty()){
int num = que.size();
List<Integer> tem = new ArrayList<>();
while(num>0){
TreeNode node = que.poll();
tem.add(node.val);
if(node.left!=null)
que.offer(node.left);
if(node.right!=null)
que.offer(node.right);
num--;
}
list.add(tem);
}
return list;
}
}