public class BinaryTree {
TreeNode root;
public BinaryTree(T array[]) {
root = createBinaryTree(array, 0);
}
/**
* @param array
* @param i
*
* Description:
*
*/
private TreeNode createBinaryTree(T[] array, int index) {
TreeNode node = null;
if (index < array.length) {
node = new TreeNode(array[index]);
node.left = createBinaryTree(array, index * 2 + 1);
node.right = createBinaryTree(array, index * 2 + 2);
// System.out.println(node.value);
return node;
}
return node;
}
/**
*
* @param root
* @return
*
* Description:二叉树的高度
*
*/
public int getDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
}
/**
*
* @param root
* @return
*
* Description:查找所有的叶子节点
*
*/
public List> getAllLeafNode(TreeNode root) {
List> list = new ArrayList>();
if (root == null) {
return null;
}
Queue> queue = new LinkedList>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.left == null && node.right == null) {
list.add(node);
}
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return list;
}
/*
* 平衡二叉树:左右子树的高度差<=1
*/
public boolean isBalancedTree(TreeNode root) {
if (root == null) {
return false;
}
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if (Math.abs(leftDepth - rightDepth) <= 1) {
return true;
}
return false;
}
public TreeNode reverseTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode right = root.right;
root.right = reverseTree(root.left);
root.left = reverseTree(right);
return root;
}
/**
* 广度优先遍历
*
* @param root
*
* Description:
*
*/
public void levelOrderTraversal(TreeNode root) {
if (root == null) {
return;
}
Queue> queue = new LinkedList>();
TreeNode currentNode = null;
queue.add(root);
while (!queue.isEmpty()) {
currentNode = queue.poll();
System.out.println(currentNode.value);
if (currentNode.left != null) {
queue.add(currentNode.left);
}
if (currentNode.right != null) {
queue.add(currentNode.right);
}
}
}
/**
* 深度优先遍历
*
* @param root
*
* Description:树的先根遍历的非递归实现和深度优先一模一样
*
*/
public void depthOrderTraversal(TreeNode root) {
if (root == null) {
return;
}
Stack> stack = new Stack>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode currentNode = stack.pop();
System.out.println(currentNode.value);
if (currentNode.right != null) {
stack.push(currentNode.right);
}
if (currentNode.left != null) {
stack.push(currentNode.left);
}
}
}
// 先根
public void preOrderTraversal(TreeNode root) {
if (root == null) {
return;
}
System.out.println(root.value);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
// 中根
public void inOrderTraversal(TreeNode root) {
if (root == null) {
return;
}
inOrderTraversal(root.left);
System.out.println(root.value);
inOrderTraversal(root.right);
}
// 后根
public void postOrderTraversal(TreeNode root) {
if (root == null) {
return;
}
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.println(root.value);
}
//后根遍历的非递归 先根 根-->左子树-->右子树 调整为 根--》右子树--》左子树 即为后根的逆序
//后根遍历,左子树--》右子树--》根
public List postOrderTraversalunrecursive(TreeNode root) {
List list=new ArrayList<>();
Stack stack=new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.pop();
list.add(node);
if(node.left!=null){
stack.push(node.left);
}
if(node.right!=null){
stack.push(node.right);
}
}
Collections.reverse(list);
return list;
}
public TreeNode getFather(TreeNode root,TreeNode aNode,TreeNode bNode){
if(root ==null || aNode.equals(root) || bNode.equals(root)){
return root;
}
TreeNode left=getFather(root.left, aNode, bNode);
TreeNode right=getFather(root.right, aNode, bNode);
if(left!=null && right !=null){
return root;
}
if(left!=null){
return left;
}
else
return right;
}
public static void main(String args[]) {
Integer a[] = { 1, 2, 3, 4, 5, 6 };
BinaryTree tree = new BinaryTree(a);
tree.depthOrderTraversal(tree.root);
System.out.println("depth:" + tree.getDepth(tree.root));
tree.levelOrderTraversal(tree.root);
System.out.println("--------------");
tree.preOrderTraversal(tree.root);
System.out.println(tree.isBalancedTree(tree.root));
TreeNode reverse = tree.reverseTree(tree.root);
tree.depthOrderTraversal(reverse);
System.out.println("preOrder--------------");
tree.preOrderTraversal(reverse);
System.out.println("inOrder--------------");
tree.inOrderTraversal(tree.root);
System.out.println("preOrder--------------");
tree.postOrderTraversal(tree.root);
}
}
查找所有的左叶子节点
public Set getAllLeftLeafNode(TreeNode root,Boolean flag,Set set){
if(root == null){
return null;
}
if(root.left!=null){
flag=true;
//System.out.println("l:"+root.left.value+","+flag);
getAllLeftLeafNode(root.left,flag,set);
}
if(root.right!=null){
flag=false;
//System.out.println("r:"+root.right.value+","+flag);
getAllLeftLeafNode(root.right,flag,set);
}
if(root.left==null && root.right==null && flag==true){
System.out.println(root.value);
set.add(root);
}
return set;
}
Find Bottom Left Tree Value (Easy)
Input:
1
/ \
2 3
/ / \
4 5 6
/
7
Output:
7
public T findBottomLeftLeaf(TreeNode root){
if(root==null){
return null;
}
Queue queue=new LinkedList();
queue.add(root);
TreeNode node=null;
while(!queue.isEmpty()){
node=queue.poll();
if(node.right!=null){
queue.add(node.right);
}
if(node.left!=null){
queue.add(node.left);
}
}
return node.value;
}