求二叉树的最大深度、最小深度、反转二叉树及判断是否为平衡二叉树

该例主要实现二叉树深度的相关问题,如最大深度、最小深度、反转二叉树及平衡二叉树

(Java版本)

public class TreeNode {

	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x){
		val = x;
	}
	
	TreeNode(){
		
	}
	
	//创建结点
	static TreeNode CreateTreeNode(int value){
		TreeNode pNode = new TreeNode();
		pNode.val = value;
		pNode.left = null;
		pNode.right = null;
		return pNode;
	}
	
	//连接二叉树
	static void ConnectTreeNode(TreeNode pParent, TreeNode pLeft, TreeNode pRight){
		if(pParent != null){
			pParent.left = pLeft;
			pParent.right = pRight;
		}
	}
	
	//求二叉树的最大深度
	static int MaxTreeDepth(TreeNode root){
		if(root == null) return 0;
		
		int left = 1;
		int right = 1;
		left += MaxTreeDepth(root.left);
		right += MaxTreeDepth(root.right);
		
		return left>right?left:right;
	}
	
	//求二叉树的最小深度
	static int MiniTreeDepth(TreeNode root){
		if((root == null)) 
			return 0;
		if((root.left == null) && (root.right == null))
			return 1;
		
		int left = MiniTreeDepth(root.left);
		int right = MiniTreeDepth(root.right);
		
		if(left == 0) 
			return right + 1;
		else if(right == 0)
			return left + 1;
		else 
			return Math.min(left, right) + 1;
	}
	
	//反转一棵二叉树
	static TreeNode invertTree(TreeNode root){
		if(root == null) return null;
		TreeNode tempNode = root.left;
		root.left = invertTree(root.right);
		root.right = invertTree(tempNode);
		return root;
	}
	
	//判断该二叉树是否为平衡二叉树
	public static boolean isBalanced(TreeNode root){
		if(root == null) 
			return true;//空树是平衡二叉树
		
		if(root.left==null && root.right==null) 
			return true;//只有一个节点是平衡二叉树
		
		if(Math.abs(getDepth(root.left) - getDepth(root.right)) > 1)
			return false;
		
		return isBalanced(root.left)&&isBalanced(root.right);
	}
	public static int getDepth(TreeNode root){
		if(root == null)
			return 0;
		return 1+Math.max(getDepth(root.left), getDepth(root.right));
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//      1
		//   /      \
		//  2        3
		// /\         \
		//4  5         6
		//     /
		//  7
		TreeNode pNode1 = CreateTreeNode(1);
		TreeNode pNode2 = CreateTreeNode(2);
		TreeNode pNode3 = CreateTreeNode(3);
		TreeNode pNode4 = CreateTreeNode(4);
		TreeNode pNode5 = CreateTreeNode(5);
		TreeNode pNode6 = CreateTreeNode(6);
		TreeNode pNode7 = CreateTreeNode(7);		
		
		
		ConnectTreeNode(pNode1, pNode2, pNode3);
		ConnectTreeNode(pNode2, pNode4, pNode5);
		ConnectTreeNode(pNode3, null, pNode6);
		ConnectTreeNode(pNode5, pNode7, null);
		
		System.out.println("Tree max depth = " + MaxTreeDepth(pNode1));
		System.out.println("Tree min depth = " + MiniTreeDepth(pNode1));
		
		System.out.println("Is balanced tree:" + isBalanced(pNode1));
	}

}

(Golang版本)-- 二叉树的最小深度

  • BFS框架解题
package main

import (
	list2 "container/list"
	"fmt"
)

// 二叉树结构
type BTree struct {
	Val   int
	Left  *BTree
	Right *BTree
}

// 构建一棵二叉树
//      8
//    /   \
//   6     10
//  / \
// 5   7
func newBTree() *BTree {
	node2_1 := &BTree{Val: 5}
	node2_2 := &BTree{Val: 7}
	node1_1 := &BTree{Val: 6, Left: node2_1, Right: node2_2}
	node1_2 := &BTree{Val: 10}
	root := &BTree{Val: 8, Left: node1_1, Right: node1_2}
	return root
}

/*
 * 求二叉树最小深度
 */
func minDepth(root *BTree) int {
	if root == nil {
		return 0
	}

	q := newQueue()
	q.push(root)
	depth := 0

	for q.len() > 0 {
		node := q.pop().(*BTree)
		if node.Left == nil && node.Right == nil { // 到达第一个叶子节点
			return depth
		}

		if node.Left != nil {
			q.push(node.Left)
		}
		if node.Right != nil {
			q.push(node.Right)
		}
		depth++
	}
	return depth
}

func main() {
	root := newBTree()
	fmt.Println("minDepth=", minDepth(root))
}


// 自定义队列
type myQueue struct {
	List *list2.List
}

func newQueue() *myQueue {
	queue := myQueue{List: list2.New()}
	return &queue
}

func (queue *myQueue) push(ele interface{}) {
	queue.List.PushBack(ele)
}

func (queue *myQueue) pop() interface{} {
	if ele := queue.List.Front(); ele != nil {
		queue.List.Remove(ele)
		return ele.Value
	}
	return nil
}

func (queue *myQueue) len() int {
	return queue.List.Len()
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值