输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.
解析:
import java.util.ArrayList;
import java.util.Stack;
class TreeNode{
int val =0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
//递归实现路径的遍历,用栈记录访问过的节点。
public class findBinaryTreePath {
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target){
ArrayList<ArrayList<Integer>> pathList = new ArrayList<ArrayList<Integer>>();
if(root == null)
return pathList;
Stack<Integer> stack = new Stack<Integer>();
FindPath(root,target,stack,pathList);
return pathList;
}
private void FindPath(TreeNode root, int target, Stack<Integer> stack, ArrayList<ArrayList<Integer>> pathList) {
// TODO Auto-generated method stub
if(root == null)
return;
if(root.left==null && root.right==null) {//基准条件1 递归函数内部结束
if(root.val==target) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(Integer i:stack) {
list.add(i);
}
list.add(root.val);
pathList.add(list);
}
}else { //基准条件2(判断结束递归调用) 递归演进
stack.push(root.val);
FindPath(root.left,target-root.val,stack,pathList);
FindPath(root.right,target-root.val,stack,pathList);
stack.pop();
} //无需返回值
}
}
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
解析:
import java.util.Arrays;
import java.util.Vector;
public class Solution {
public boolean VerifySquenceOfBST(int[] sequence) {
//基准条件1 内部递归函数何时结束
if(sequence==null || sequence.length<=0)
return false;
int root = sequence[sequence.length-1];
int i=0;
for(;i<sequence.length-1;i++)
if(sequence[i]>root)
break;
int j=i;
for(;j<sequence.length-1;j++){
if(sequence[j]<root)
return false;//结束1
}
boolean left = true;
boolean right = true;
//基准条件2 外部递归
if(i>0)
left = VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,i));
if(j==sequence.length-2)
right = VerifySquenceOfBST(Arrays.copyOfRange(sequence,i,sequence.length-1));
return (left && right);//返回结果,层层传递到上层
}
}
输入一颗二叉树,判断该二叉树根节点到叶子节点最短距离
public class Mini_Depth_Of_BinaryTree {
public int run(TreeNode root) {
if(root==null)
return 0;
return minDepth(root);
}
//二叉树最小深度 ?这里是不正确的,深度是到叶子节点为计算
private int minDepth(TreeNode root) {
// TODO Auto-generated method stub
if(root==null)
return 0;
if(root.left==null && root.right==null) {
return 1;
}
int left = minDepth(root.left);
int right = minDepth(root.right);
return left>right?right+1:left+1;
}
//二叉树头节点到叶子节点,三种情况下树的高度
//根节点只有左子树,只有右子树,既有左子树又有右子树
private int minDepthByleaf(TreeNode root) {
// TODO Auto-generated method stub
if(root==null)
return 0;
if(root.left==null && root.right==null) {
return 1;
}
if(root.left==null) {
return minDepthByleaf(root.right)+1;
}
if(root.right==null)
return minDepthByleaf(root.left)+1;
else
return Math.min(minDepthByleaf(root.left), minDepthByleaf(root.right))+1;
}
}
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
解析:
public class Solution {
boolean isBlanced = true;
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null)
return true;
getDepth(root);
return isBlanced;
}
private int getDepth(TreeNode root){
if (root==null) //基准条件1 递归终止
return 0;
//基准条件2 递归演进
int left = getDepth(root.left);
int right = getDepth(root.right);
if(Math.abs(left-right)>1)
isBlanced=false;
//如果 return left+right+1 一般是对左右子树高度累加,
//该种方式用于计算左右子树满足某条件,统计满足条件数量时候使用。
return left>right?left+1:right+1;//返回结果 向上传递
}
}